Reputation: 2353
I'm trying to change the numbers inside a grid into a text. Depending on the value in the cell I should get another text.
If the value = 0
it should show "inkomst"
and if the value = 1
it should show "Uitgave"
.
I already have this code to display the correct rows and to format the widths.
private void updateAll()
{
dataGridViewInkomsten.DataSource = blFinancien.getFinancienByInkomsten();
//kolommen niet tonen waarin enkel id's worden getoond
dataGridViewInkomsten.Columns[0].Visible = false;
dataGridViewInkomsten.Columns[1].Visible = false;
dataGridViewInkomsten.Columns[2].Visible = false;
dataGridViewInkomsten.Columns[8].Visible = false;
dataGridViewInkomsten.Columns[6].HeaderText = "Inkomst/uitgave";
int breedtevankolommen = dataGridViewInkomsten.Width / 5;
dataGridViewInkomsten.Columns[3].Width = breedtevankolommen;
dataGridViewInkomsten.Columns[4].Width = breedtevankolommen;
dataGridViewInkomsten.Columns[5].Width = breedtevankolommen;
dataGridViewInkomsten.Columns[6].Width = breedtevankolommen;
dataGridViewInkomsten.Columns[7].Width = breedtevankolommen;
}
Now depending on the int value in the sixth column I should get "Inkomst"
or "Uitgave"
.
I also got a printscreen, so you see what I mean.
Could anybody tell me how to get that done?
Upvotes: 1
Views: 1221
Reputation: 4797
First create cell style:
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle1.Format = "One;\'\';Zero";
dataGridViewCellStyle1.NullValue = null;
Than apply the cell style to your dynamic column:
dataGridViewInkomsten.Columns[6].DefaultCellStyle = dataGridViewCellStyle1;
That should do it.
Upvotes: 1
Reputation: 131
Try this:
SqlCeCommand cmd = new SqlCeCommand();
SqlCeConnection conn = new SqlCeConnection("connString");
cmd.Connection = conn;
cmd.CommandText = "SELECT COUNT(*) FROM yourTable";
var count = cmd.ExecuteScalar();
for (int i = 0; i <= int.Parse(count.ToString()); i++)
{
if (dataGridViewCategories.Rows[i].Cells["cellName"].Value.ToString() == "0")
{
dataGridViewCategories.Rows[i].Cells["coulmnName"].Value = "Inkomst";
}
else
{
dataGridViewCategories.Rows[i].Cells["coulmnName"].Value = "Uitgave";
}
}
Upvotes: 0
Reputation: 9074
edit you query through which you are binding your grid.
include case when in it as follows:
CASE WHEN lnkomst/uitgave = 0 THEN 'lnkomst' ELSE 'uitgave' END
select col1,col2,(CASE WHEN lnkomst/uitgave = 0 THEN 'lnkomst' ELSE 'uitgave' END) as lnkomst/uitgave from tablename
write query like this.
Edit for access:
select col1,col2,lnkomst/uitgave = Switch(
NUMREG=0,'lnkomst',
NUMREG=1,'uitgave'); from tablename
Hope it helps.
Upvotes: 0
Reputation: 331
There are a few ways to do it.
You can write a switch case in you SQL query and set the required text.
Or you can add another column in your grid. Say COLUMN2. Hide the actual column say COLUMN1.
Then in you grid cell formatting event (or any similar event) you can set the text of this column (COLUMN2) based on the value in the actual column (COLUMN1).
private sub grid_CellFormatting(sender ,e) { if(grid.Rows(e.RowIndex).Cells("COLUMN1").Text == "0") grid.Rows(e.RowIndex).Cells("COLUMN2").text = "value1" else(grid.Rows(e.RowIndex).Cells("COLUMN1").Text == "1") grid.Rows(e.RowIndex).Cells("COLUMN2").text = "value2" }
Actually you should be doing this kind of formatting for UI in your domain model and then bind you domain model to your grid. That is after you have fetched the data from database.
Upvotes: 0
Reputation: 131
After you save the data into the DB, use datagridview.rows[].cells[].value
Upvotes: 0