Groups | Blog | Home
all groups > dotnet windows forms databinding > may 2007 >

dotnet windows forms databinding : How to CHANGE a textbox column in a databound datagrid to a combob



Roger Tranchez
5/28/2007 9:53:01 AM
Hi all !,

One BASIC question about combo boxes in a data bound datagridview:

I have a datagridview with its datasource property pointing to a
bindingsource.

The bindingsource has its datasource pointing to a dataset with only one
datatable.

The datagridview loads all correctly (9 columns). The first one shows as a
textbox column.

Also, the datagridview has its autogeneratecolumns property set to true (by
default)

Till now, a typical scenario , it isn't ?

Now, what I want is to change the appereance of the automatically generated
first column to a DataGridViewComboBoxCell with two string values to choose
in, and that
will save the choosed one on the corresponding column of the
bindingsource,and finally to the dataset.

I tried to change it, but when applying the DataGridViewComboBoxCell
template to my already automatically created column it says that datagrid
text box is the only type it accepts...

It is possible to change this column without having to define ALL THE
COLUMNS FROM THE START (autogeneratecolumns=true) ?

I suppose that if it is the answer, I'd have to bind individually all the
datasource property for the columns to each of the "columns" of the
bindingsource, but i'm not sure... for the filter, not sure too


Thanks in advance and sorry for making this very basic question !


--
Roger Tranchez
v-lliu NO[at]SPAM online.microsoft.com
5/29/2007 3:04:02 AM
Hi Roger,

The DataGridView.AutoGenerateColumns property indicates whether columns are
created automatically when the DataSource or DataMember properties are set.

If the AutoGenerateColumns property is set to false, the columns won't be
generated automatically while the the DataSoure or DataMember properties
are set.

If the AutoGenerateColumns property is set to true, when the DataSource or
DataMember properties are set, DataGridView will automatically generate a
column for each field in the data source that hasn't been bound to the
existing columns in the DataGridView .

For example, we have a DataSet containing a DataTable, and the DataTable
has three fields--ID, Name and Age in it. We have a DataGridView and add a
DataGridViewTextBoxColumn into it. We set the DataGridViewTextBoxColumn's
DataPropertyName property to "ID" and then set the DataGridView's
DataSource property to the DataSet and DataMember property to the DataTable
name.

Only two columns are generated automatically for the "Name" and "Age"
fields respectively, because the "ID" field has been bound to the existing
DataGridViewTextBoxColumn in the DataGridView.

As for your question, I suggest that you add a DataGridViewComboBoxColumn
in the DataGridView and set the DataPropertyName property of the column to
the field in the data source you'd like to bind to BEFORE you set the
DataSource or DataMember properties of the DataGridView.

The following is a sample code snipt.

DataGridViewComboBoxColumn column1 = new
DataGridViewComboBoxColumn();
column1.Items.Add("1");
column1.Items.Add("2"); // you could also set the column's
DataSource property
column1.DataPropertyName = "ID";
this.dataGridView1.Columns.Add(column1);

this.dataGridView1.DataSource = this.dataSet11;
this.dataGridView1.DataMember = "TableName";

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Roger Tranchez
6/8/2007 10:13:01 AM
Hello, thanks for your reply.

After loading columns, I can also reorder them with
..Columns("colname").DisplayIndex=n

Thanks again,
--
Roger Tranchez
MCTS
..NET 2005 and DB developer


[quoted text, click to view]
AddThis Social Bookmark Button