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?
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();
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.