Click here to Skip to main content
16,016,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Trying to fill the datagridviewcomboboxcell using datagridview.rows.add method. It shows the first value instead of the actual value. But if i Read the value of the current cell then it shows the correct value. if i don't use the event DataError then it shows me exception "datagridviewcomboboxcell value is not valid".

Here is my code

C#
private void AutoCompleteCustomer()
      {
               foreach (DataGridViewRow row in dgvCustomer.Rows)
           {
               DataGridViewComboBoxCell nameCell = ((DataGridViewComboBoxCell)row.Cells["colCustomerName"]);
               nameCell.Items.Clear();
               var custInfo = newDB.CustomerTbls.Where(s => s.Status == "Active" & s.Type == "Customer").Select(s => s);
               foreach (var c in custInfo)
               {
                   nameCell.Items.Add("(" + c.Id + ") " + c.Name);
               }
           }
       }

   private void dgvCustomer_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
   {
       AutoCompleteCustomer();
   }

here is the Fill Code which fills the datagridview on form load

C#
private void FillForm()
    {
        lblInvoiceNumber.Text = InvoiceNumber;
        lblItemName.Text = leslieDB.GAItemTbls.Where(i => i.Status == "Active" & i.PartNo == _partNo).Select(i => i.Name).Single();
        var relInfo = from r in leslieDB.GARelationTbls.Where(r => r.Item_PartNo == _partNo & r.InvoiceNumber == InvoiceNumber & r.Status == "Active")
                      join c in leslieDB.CustomerTbls on r.Customer_Id equals c.Id
                      where c.Status == "Active" & c.Type == "Customer"
                      select new
                     {
                         r.Id,
                         Name = c.Name,
                         cId = c.Id,
                         r.Quantity
                     };
        foreach (var re in relInfo)
        {
            dgvCustomer.Rows.Add(re.Id, "(" + re.cId + ") " + re.Name, re.Quantity);
        }
    }
Posted

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



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