簡體   English   中英

綁定后更改Datagridview中的值

[英]change values in Datagridview when it has been binded

在我的項目中,我嘗試將其與DataSet綁定后更改DataGridView列中的值。 像這樣:

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)
        }
    }                            
 }

我的解決方案是成功輸入if語句,甚至txt具有正確的值,但是我不知道r.Cells[4]發生了什么,它具有相同的舊值。
盡管網格列中的值沒有更改,但一切都正確。如何更改它們?

您需要使用DataGridView CellFormatting事件:

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();
             }
         }
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM