Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 23rd, 2009, 10:42 AM
Registered User
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to set default value in DataGridViewComboBox during DataGridViewControl Loading..

I am using datagridview control in my application. The datagridview control consist of two textbox column and one combobox column,Where combobox is loaded with some set of data in database is working correctly. But the issue is i want to select a default value in the combobox while loading. Please help us to solve the issue.
Old January 24th, 2009, 02:26 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Not much detail to work with. Are you using data binding? If so, what sort of object are you binding to? I was doing something similar recently that involved binding to a custom list of custom key/value pair objects that you might find helpful. The combobox simply displays the first item in the list as a default value. The key/value pair objects expose two public properties named (amazingly) Key and Value. After binding the DataGridViewComboBoxColumn to a BindingSource object you can do this:

private void DisplayFirstItemInComboColumn()
 DataGridViewComboBoxColumn column = (DataGridViewComboBoxColumn) 
 for (int x = 0; x <= LineItemsDataGridView.Rows.Count - 1; x++)
    DataGridViewCell cell = LineItemsDataGridView.Rows[x].Cells["Product"];
    if (column.Items.Count > 0)
       cell.Value = (KeyValuePair)column.Items[0];
The DataGridViewComboBoxColumn is configured like this:

this.Products.DataSource = this.ProductListBindingSource;
this.Products.DisplayMember = "Value";
this.Products.HeaderText = "Product";
this.Products.Name = "Product";
this.Products.ValueMember = "Key";
Anyway, you're basically just getting a reference to a DataGridViewCell object, then setting its Value property.



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i set default parameter value.. rupen Classic ASP Basics 5 January 5th, 2007 12:14 AM
Set Default selection in ListBox nmreid Classic ASP Basics 0 January 20th, 2006 04:49 PM
XMLDOM default character set sonicDace XML 0 May 21st, 2004 03:09 PM
how to set parameter default value oranginalab Classic ASP Basics 0 August 28th, 2003 02:53 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.