Click here to Skip to main content
14,698,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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++;

  
 }
Posted
Updated 14-Nov-17 19:30pm
v3

1 solution

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
   
Comments
Graeme_Grant 15-Nov-17 0:39am
   
Please don't post updates as solutions to the question. This will confuse all. Instead delete this solution and click on Improve question widget to add more info to the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900