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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Don't use an else if . . . use 2 if statements as you want to evaluate both conditions.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.