Reputation: 41832
In my project, I am trying to change values in a column of DataGridView after binding it with DataSet. Something like this:
dgvMain --> DataGridView
dsMainPatients --> DataSet
dsMainDoctors --> DataSet
dgvMain.DataSource = null;
dgvMain.DataSource = dsMainPatients.Tables[0];
foreach (DataGridViewRow r in dgvMain.Rows)
{
DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
for (int intCount = 0; intCount < dsMainDoctors.Tables[0].Rows.Count; intCount++)
{
if (r.Cells[4].Value.ToString().Equals(dsMainDoctors.Tables[0].Rows[intCount][0].ToString()))
{
txt.Value = dsMainDoctors.Tables[0].Rows[intCount][1].ToString();
r.Cells[4] = txt; //dgvMain[4, r.Index] = txt; (also tried)
}
}
}
My solution is entering in to the if statement successfully and even txt
holds correct value but I dont know what is happening at r.Cells[4]
that it has the same old previous value.
Everything is correct eventhough the value in the grid columns are not changing.. How to change them?
Upvotes: 0
Views: 2204
Reputation: 3063
You need to use CellFormatting
event of DataGridView
:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 4)
{
for (int intCount = 0; intCount < dsMainDoctors.Tables[0].Rows.Count; intCount++)
{
if (e.Value.ToString().Equals(dsMainDoctors.Tables[0].Rows[intCount][0].ToString()))
{
e.Value = txt.Value = dsMainDoctors.Tables[0].Rows[intCount][1].ToString();
}
}
}
}
Upvotes: 1