Avatar of Gani tpt
Gani tpt
 asked on

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..?
C#

Avatar of undefined
Last Comment
Gani tpt

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kyle Abrahams

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gani tpt

ASKER
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.
Gani tpt

ASKER
thanks.it's working.
Your help has saved me hundreds of hours of internet surfing.
fblack61