Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Combobox in Gridview help
 

Combobox in Gridview help

So I have a grid...

The first field in it is a combobox field whose Items property is filled with Product objects (consisting of ProductID (int) and CoverageType (string)).

Whan I try to assign existing records to the grid, I'm doing this:

internal void FillFields(List<QuoteDetail> QD)
{
string[] row = new string[ 8 ];

// loop through the passed in List<>
foreach (QuoteDetail q in QD)
{
row[0] = q.Product.CoverageType.ToString();
row[1] = q.Quantity.ToString();
row[2] = q.Product.Per.ToString();
row[3] = q.Product.UnitOfMeasure.ToString();
row[4] = q.Product.StandardMarkup.ToString();
row[5] = q.Markup.ToString();
row[ 6 ] = q.Product.Rate.ToString();
row[7] = q.Product.TerrorismMarkup.ToString();

// bind each row of the List<> to the grid
gridRateRecords.Rows.Add(row);

} // end foreach

// assign grid's columns[#] to cooresponding List<> column.
gridRateRecords.Columns[0].DisplayIndex = 0;
gridRateRecords.Columns[1].DisplayIndex = 1;
gridRateRecords.Columns[2].DisplayIndex = 2;
gridRateRecords.Columns[3].DisplayIndex = 3;
gridRateRecords.Columns[4].DisplayIndex = 4;
gridRateRecords.Columns[5].DisplayIndex = 5;
gridRateRecords.Columns[ 6 ].DisplayIndex = 6;
gridRateRecords.Columns[7].DisplayIndex = 7;
}

When the form displays, I get a DataGridView Default Error Dialog with "DataGridViewComboBoxCell value is not valid". After I click OK for each record, the grid displays find, except for the combo box, which is blank. If I try to click or even hover inside one of the combo boxes. This error reappears.


ANY help would be appreciated. I'm at a complete loss.
Sithlrd  Monday, September 17, 2007 9:34 PM

Hi, yup I got what you meant and I think I know the problem:

you should not use:

Code Snippet
row[0] = q.Product.CoverageType.ToString();

Instead, do something like

Code Snippet
row[0] = q.Product.ProductID;

Because in this case, the ProductID is the value to link to the CoverageType. Hope it helps this time..
Yun Feng  Tuesday, September 18, 2007 5:23 PM

Code Snippet

dataGridViewComboBoxColumn.DataPropertyName = "ProductID";

dataGridViewComboBoxColumn.DataSource = this.bindingSourceProduct;

dataGridViewComboBoxColumn.DisplayMember = "CoverageType";

dataGridViewComboBoxColumn.ValueMember = "ProductID";

Hi, do you mean you set the dataGridViewComboBoxColumn for the first field with these settings? Or do you mean you insert all CoverageType into the Items property one by one? Filled with Product objects mean?

The error occurs when your row has a value which is not found inside the dataGridViewComboBoxColumn's Item of when your row has a value not found in the dataGridViewComboBoxColumn.ValueMember.

Yun Feng  Tuesday, September 18, 2007 5:04 AM
Thanks for the reply. Sorry for the confusion, I probably should have also mentioned that I'm populating the combo box column Items list thusly:
Code Snippet

foreach (PWS.Product p in Prods)
{
cboProduct.Items.Add(p);
} // end adding products


When the form launches, I'm just pulling a list of all my Products (which contains the public properties ProductID and CoverageType, and Add-ing them to the Items list. It works fine for the free-standing combo boxes on other forms. But for some reason it's not working well with the Grid-based combo box.
Sithlrd  Tuesday, September 18, 2007 12:47 PM

Hi, how about trying this:

Code Snippet

foreach (PWS.Product p in Prods)

{

cboProduct.Items.Add(p.CoverageType);

}

Does this solve the problem?

Yun Feng  Tuesday, September 18, 2007 1:18 PM
I'm not gaining anything by doing that, since the vaule coming in from the database for this field is the ProductID (an int). CoverageType is just the friendly name for it. In fact the comands preceding the foreach are:

// set the source & display columns
cboProduct.DisplayMember = "CoverageType";
cboProduct.ValueMember = "ProductID";

Basically I need to list all available products in the dropdown and then have it automatically select the right one out of the list when I start filling in records. You know, the stuff that Access Datagrids have been doing for 15 years!

I'm too old for this much frustration. Smile
Sithlrd  Tuesday, September 18, 2007 5:00 PM

Hi, yup I got what you meant and I think I know the problem:

you should not use:

Code Snippet
row[0] = q.Product.CoverageType.ToString();

Instead, do something like

Code Snippet
row[0] = q.Product.ProductID;

Because in this case, the ProductID is the value to link to the CoverageType. Hope it helps this time..
Yun Feng  Tuesday, September 18, 2007 5:23 PM

I'm afraid that command is what caused this error in the first place. Smile

After much in-my-head debate, forum searches, and even attempts at using third party controls, I've decided to listen to that little voice int he back of my head that has said during the last 6 weeks "You could do this in Access in 2 weeks. Cmon, Access has been good to you. Sure c# is nice to look at, but it's grid was coded by cracksmoking baboons. Access just works. Has worked for 15 years. Come on back to Access. It is your destiny..."

You get the idea. Looks like my personal Lord Vader is right. If it needs a grid, then it needs Access.

Sithlrd  Wednesday, September 19, 2007 5:05 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView merge columns headers
• Problem running delete query on remote server
• datagrid's data view, currency manager, and binding context
• Table relationships not showing up in Data Source - what gives?
• C# and TreeNode - please help out...
• On a DataGridView control, is it possible to ...
• How to Change DataView's Default Sort Comparer
• How to handle dagagridview cell copy and paste
• Lauucher Form like deskbar
• Click data grid row head, what event to can I code it?