Simon Martin

I am trying to populate a DataGridViewComboBoxColumn but I get nothing in the ComboBoxColumn and I can't see why. This first code block shows how I am creating the column and the 2 approaches I have tried to populate it (the foreach is commented out ). And then I add the DataGridViewComboBoxColumn to dataGridView1 (there are other columns that I am adding, but I have omitted them to keep the code down here).

Code Block

DataGridViewComboBoxColumn saleTypeCol = new DataGridViewComboBoxColumn();
saleTypeCol.HeaderText = "Sale Type";
saleTypeCol.Name = "SaleType";
saleTypeCol.ReadOnly = false;
saleTypeCol.DropDownWidth = 100;

//TODO must get this to bind saletypes to the drop down list
saleTypeCol.ValueMember = "SaleType";
saleTypeCol.DisplayMember = "SaleType";
saleTypeCol.DataSource = controller.GetSaleTypes();
//foreach (CartInfo objCart in controller.GetSaleTypes())
//{
// saleTypeCol.Items.Add(objCart.SaleType);
//}



dataGridView1.Columns.AddRange(new DataGridViewComboBoxColumn[] { saleTypeCol });


And then after the columns have been added I create a new row for each item in my cart and bind the values to the cells

Code Block

foreach (CartInfo objCart in arrCheckoutItems)
{
grandTotal += Decimal.Parse(objCart.ProductInfo.ItemPrice.ToString()) * int.Parse(objCart.Quantity.ToString());
dataGridView1.Rows.Add(objCart.ProductID, objCart.ProductInfo.ProductName, objCart.ProductInfo.SupplierName, objCart.ProductInfo.CategoryName, "", String.Format("{0:F2}", objCart.ProductInfo.ItemPrice), objCart.Quantity.ToString(), String.Format("{0:F2}", Convert.ToDecimal(objCart.ProductInfo.ItemPrice * objCart.Quantity)));
}
dataGridView1.Rows.Add("", "", "", "", "", "", "Total:", String.Format("{0:C}", grandTotal));


Where am I going wrong



Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Zhi-Xin Ye - MSFT

When you click on the ComboBox, isn't there any item in the drop down list What kind of data type do you retrieve through controller.GetSaleTypes() method Make sure this method returns any items or not.






Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Simon Martin

When I click on the ComboBox it opens and shows nothing. I thought that might be because in my foreach loop I pass an empty string for that item, but when I try passing an array of SaleTypes directly it errors.

My GetSaleTypes() method returns a very simple collection, SaleTypeID is the Primary Key and SaleType is a VARCHAR(15). I can bind the values it returns to a ComboBox elsewhere on the page and it returns the values I expect e.g.
1 Sale
2 Repair
3 Commission




Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Zhi-Xin Ye - MSFT

You still don't say what kind of data type that the GetSaleTypes() method returns. Anyway, you can put a breakpoint at this line of code:

saleTypeCol.DataSource = controller.GetSaleTypes();

and debug it to see if any data has been retrieved.






Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Simon Martin

GetSaleTypes() returns a public List<CartInfo>.
This is my 1st Winform app and I've never used the breakpoints before. I have set one at the line but I'm not really sure what I'm looking for after that. I assume I should use the step into command to move through my code - but what should I be looking for and where would I find it. (Sorry if that's a really obvious thing )

Code Block

public class CartInfo
{
private int _productID = 0;
public int ProductID
{
get { return _productID; }
set { _productID = value; }
}

private int _quantity = 0;
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}

private int _saleTypeID = 0;
public int SaleTypeID
{
get { return _saleTypeID; }
set { _saleTypeID = value; }
}

private String _saleType = "";
public String SaleType
{
get { return _saleType; }
set { _saleType = value; }
}

private ProductInfo _productInfo = null;
public ProductInfo ProductInfo
{
get { return _productInfo; }
set { _productInfo = value; }
}
}


Simon
-----------------------------
Sharing knowledge saves time!




Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Zhi-Xin Ye - MSFT

You can take the following steps to debug and check:

1). set a breakpoint at:

saleTypeCol.DataSource = controller.GetSaleTypes();

2). Press F5 to debug.
3). Press F10 to step over.
4). When the program run to the line, move your mouse over GetSaleTypes(), and check if any data is retrieved by reviewing the data tip window.






Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Simon Martin

Sorry for the delay in replying

I have tried to debug as you described, but I'm not getting anything when I move my mouse over GetSaleTypes() in step 4 - though that may be me doing something wrong

After debugging the line gets highlighted in yellow, I press F10 and the line turns dark red and a line further down turns yellow and has an arrow alongside. Moving my mouse over GetSaleTypes() does nothing, but I can hover my mouse over bits of the yellow line and get something come up (which I assume is the data tip window ) - though I'm not really sure what most of it means.

However that suggested to me that there wasn't anything being returned by my method, so I used a MessageBox.Show to fint out the count and it said 0, confirming your suggesting that the method isn't returning anything.
I am going to re-write the method and see if that sorts things out.

I'll hunt down a tutorial on debugging / breakpoints - is there anything in particular you think I should look for, besides a general introduction on how-to





Re: Windows Forms Data Controls and Databinding DataGridViewComboBoxColumn binding

Simon Martin

Ok, Seems like my little break has refreshed my brain and I've found out what was going on.
GetSaleTypes() was looping through a SqlDataReader and creating a new SaleTypeInfo object for each row but then not adding that row to the List<SaleTypeInfo> so the list was always returning empty
SaleTypeInfo replaces my CartInfo class and only has properties for SaleTypeID and SaleType.

So GetSaleTypes() now returns a List<SaleTypeInfo>. However when trying to use that to bind to the DataGridViewComboBoxColumn I was still getting an error. Not sure why but neither an ArrayList nor the List<SalesTypeInfo> was able to bind, so I ended up using a foreach loop to add the items:

Code Block

foreach (SaleTypesInfo objSaleTypes in controller.GetSaleTypes())
{
saleTypeCol.Items.Add(objSaleTypes);
saleTypeCol.ValueMember = "SaleTypeID";
saleTypeCol.DisplayMember = "SaleType";
}


and that has fixed the problem

I am a little dubious about marking my own post as the answer, but it does solve the problem so for anyone in the same situation this might help