C# - datagridview组合框单元格未正确更新。 [英] C# - datagridview combo box cell not updating properly.

查看:82
本文介绍了C# - datagridview组合框单元格未正确更新。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用datagridview。



i只想更新组合框单元值。我的单元格值应该根据我的条件更新。



但是,问题是,组合框值显示最后执行的值只是始终。提醒条件是更新单元格。



我的代码中有什么问题。



样本数据。



Hi,

I am using datagridview.

i just want to update the combobox cell value. i cell value should update based on my condition.

But, the problem is, combobox value displaying which is last executed value only always. the remining condition is updating the cell.

what is the problem in my code.

sample data.

EMPNO	STATUS (Dropdown)
101	1-EXCELLENT 2-MODERATE
102	1-EXCELLENT 2-MODERATE 3-GOOD 4-AVERAGE
103	1-EXCELLENT 2-MODERATE 3-GOOD 4-AVERAGE
104	1-EXCELLENT 2-MODERATE
105	1-EXCELLENT 2-MODERATE 3-GOOD 4-AVERAGE
106	1-EXCELLENT 2-MODERATE 3-GOOD 4-AVERAGE





我试过的:





What I have tried:

List<string> Emplist = new List<string>(); 
foreach (DataRow row in dt.Rows)
 {
         Grid1.Rows.Add();
         Grid1[0, cnt].Value = row["EmpNO"].ToString();
         Grid1.UpdateCellValue(0, cnt);
         DataGridViewComboBoxCell cbobox = (DataGridViewComboBoxCell)Grid1[1, cnt];
	 
	 if (cbobox.Items.Count == 0)
         {
		// Conditions
	   
	 if ((Mark1 != 0) && (SUB == "H1"))
         {
             Emplist.Clear();
             Emplist.Add("1-EXCELLENT");
             Emplist.Add("2-MODERATE");
             cbobox.DataSource = Emplist;
             Grid1.UpdateCellValue(3, cnt);

         }
	 else if ((Mark2 != 0) && (SUB2 == "H2"))
         {
             Emplist.Clear();
             Emplist.Add("1-EXCELLENT");
             Emplist.Add("2-MODERATE");
	     Emplist.Add("3-GOOD");
             Emplist.Add("3-AVERAGE");
             cbobox.DataSource = Emplist;
             Grid1.UpdateCellValue(3, cnt);

         }


	 }

	 Grid1.UpdateCellValue(1,cnt);
         Grid1.Refresh();
         cnt++;

  
 }

推荐答案

Thanks.

i tried one more ideas.

I have added different list item to map the combo box cell. it's also working.

Example

List<string> Emplist1 = new List<string>(); 

List<string> Emplist2 = new List<string>(); 

foreach (DataRow row in dt.Rows)
 {
         Grid1.Rows.Add();
         Grid1[0, cnt].Value = row["EmpNO"].ToString();
         Grid1.UpdateCellValue(0, cnt);
         DataGridViewComboBoxCell cbobox = (DataGridViewComboBoxCell)Grid1[1, cnt];
       
       if (cbobox.Items.Count == 0)
         {
            // Conditions
         
       if ((Mark1 != 0) && (SUB == "H1"))
         {
             Emplist.Clear();
             Emplist.Add("1-EXCELLENT");
             Emplist.Add("2-MODERATE");
             cbobox.DataSource = Emplist1;
             Grid1.UpdateCellValue(3, cnt);

         }
       else if ((Mark2 != 0) && (SUB2 == "H2"))
         {
             Emplist.Clear();
             Emplist.Add("1-EXCELLENT");
             Emplist.Add("2-MODERATE");
           Emplist.Add("3-GOOD");
             Emplist.Add("3-AVERAGE");
             cbobox.DataSource = Emplist2;
             Grid1.UpdateCellValue(3, cnt);

         }


       }

       Grid1.UpdateCellValue(1,cnt);
         Grid1.Refresh();
         cnt++;

  
 }


This is also working and simple


这篇关于C# - datagridview组合框单元格未正确更新。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆