C# - Datagridview combo box cell not updating properly.

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

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++;

  
 }

Open in new window


what is the problem in my code.

my combobox should update both values based on my condition. But, it will update only one value always..??

what is the problem in my code..?
Ganesh STech Lead cum developerAsked:
Who is Participating?
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Don't use an else if . . . use 2 if statements as you want to evaluate both conditions.
1
 
Ganesh STech Lead cum developerAuthor Commented:
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.
0
 
Ganesh STech Lead cum developerAuthor Commented:
thanks.it's working.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.