Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DataGridView ComboBox to show different data
Message
 
To
05/12/2006 17:58:37
Jeff Corder
Ambit Technologies, LLC
Missouri, United States
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01175067
Message ID:
01175457
Views:
7
Jeff,

>I've tried everything I can think of. It's time to pick some better brains than mine.
>
>I have a DataGridView which shows items on an invoice. One column is Units of Measure (Each, Box, Dozen...). The Units of Measure for each item may be different. Also, depending on the Unit of Measure, the price, cost and a few other values change. I could just put the Unit of Measure string into the combobox, but I'd be making database hits constantly to update the prices.
>
>I tried binding a business object to each combobox (set when I look up the Inventory item), but it seems that when the Business Object went out of scope, it apparently closed the recordset and things went haywire (very haywire).
>
>I created an object containing the data I was interested in and added the objects to the ComboBox.Items collection. I get an error that the "DataGridViewComboBoxCell value is not valid." Since I picked the value from the list, there must be something else wrong. I overload ToString() in the object and the UOM's are displaying correctly.

If I understand your question correctly, the following instructions I just added to the Dev Guide should help you figure out what to do (I think the key here is storing the DataTable/DataSet which is the data source for the combo box as a form-level property):

  1. Right-click your DataGridView in design mode and select Edit Columns from the shortcut menu.

  2. In the Edit Columns dialog, click the Add button which launches the Add Column dialog. In the Name textbox, specify the name of the column, in the Type column, select DataGridViewComboBoxColumn, specify the Header Text, then click Add, then Close.

  3. In the Edit Columns dialog, set the DataPropertyName property to the column in the DataGridView's data source that sets the currently selected item in the combo box. Click OK to close the dialog.

  4. In the form's constructor, add code that retrieves the list of items to be displayed in the combo box and binds the data to the combo box's DataSource, DisplayMember, and ValueMember properties. Create a form-level variable to hold the DataTable or DataSet used as the data source for the combo box. For example:
    protected DataTable dtProducts;
    
    /// <summary>
    /// Constructor
    /// </summary>
    public CustomerOrdersForm()
    {
       /// Instantiate and register business objects
       this.oProduct = (Product)this.RegisterBizObj(new Product());
    
       //
       // Required for Windows Form Designer support
       //
       InitializeComponent();
    
       dtProducts = this.oProduct.GetAllData();
    
       this.ProductColumn.DataSource = dtProducts;
       this.ProductColumn.DisplayMember = "ProductName";
       this.ProductColumn.ValueMember = "ProductID";
    }


When your DataGridView displays at run time, the combo box column should be populated with items from your combo box data source and the currently selected item should be set based on the value of the underlying DataGridView data source.
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform