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

dotnet windows forms databinding : DataBinding DataGridView with AutoGenerateColumns=false


RipT
5/12/2007 1:34:01 PM
How do I use databinding with DataGridView.AutoGenerateColumns = false?

I have a DataGridView with a BindingSource as it's DataSource. The
BindingSource's DataSource is a typed DataTable in a typed DataSet. The
BindingSource in question is not raising BindingCompletes when editing a new
row in the DataGridView. When BindingSource.EndEdit() is called, the new row
is unpopulated (I looked at it by casting the DataRowView.Row to a typed row
from the DataTable).

I don't understand how Binding will work without a data source with an index
or column name into the data source and also a control and index or property
name of that control. In this case, the DataGridView has the DataSource. And
the DataGridViewColumn(s) have a "DataPropertyName".

Here is an example of what I'm doing reformatted for this post:

/*
* A section like this for each column
* ---------------------------------------
* I first tried setting DataPropertyName to "Text" (the property to which
to bind)
* Here I'm using the property as a DataMember field (the column to which to
bind)
*/
myDataGridViewTextBoxColumn1.HeaderText = "theDataTableColumnName";
myDataGridViewTextBoxColumn1.DataPropertyName = "theColumnName";
/*
* DataGridView initialization
* ------------------------------
* note that I'm setting up the columns before adding them to the DataGridView
*/
myGridView.AutoGenerateColumns = false;
myGridView.DataSource = this.m_BindingSource_PurchaseOrderLines;
myGridView.Columns.AddRange(new DataGridViewColumn[] {
myDataGridViewTextBoxColumn1,
myDataGridViewTextBoxColumn1, });

jetan NO[at]SPAM online.microsoft.com (
5/14/2007 12:00:00 AM
Hi,

Sorry, I am not sure if I understand your question completely.

Can you explain the sentence in more details?
"I don't understand how Binding will work without a data source with an
index or column name into the data source and also a control and index or
property name of that control"

Your code snippet logic looks correct to me. Once your 2 columns'
DataPropertyName property is correctly set to corresponding ColumnName in
the DataTable, the DataGridView will bind and display the data without any
problem. Can you tell me what is the problem here?

I wonder if it is possible for you to create a little sample project to
demonstrate your problem? I think it will help me to understand your
problem more accurate and efficient.

Btw: the best detailed document for DataGridView is "DataGridView FAQ"
below written by .Net Winform team(its original link is missing now), you
may use it to understand DataGridView model better:
http://www.devolutions.net/articles/DataGridView.aspx

Anyway, I will wait for your further reply, thanks.

Best regards,
Jeffrey Tan
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.

jetan NO[at]SPAM online.microsoft.com (
5/16/2007 3:50:04 AM
Hi,

Have you reviewed my reply to you? How about your problem now? Can you
provide more background information to help me understand it completely?
Thanks.

Best regards,
Jeffrey Tan
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.
RipT
5/18/2007 2:47:00 PM
Hi Jeffrey, thanks for helping me with this. I'm going to reply in-line.

[quoted text, click to view]
* The way I look at Windows Forms Binding is like this:
A binding describes the synchronization between two properties.
Typically these properties are of different objects.
In order to bind objA.F to objB.F the Binding object must have pointers (or
similar) to objA and objB, and an offset (or similar) to the F fields of each
object (which may be of different types).
Of course, there may be defaults or other ways of the Binding object to know
which fields to sync.

[quoted text, click to view]
* Does this mean that there is a default property to be bound specified in
the implementations of DataGridViewTextBoxColumn and
DataGridViewComboBoxColumn, etc.? E.g. data bound to a
DataGridViewTextBoxColumn through the DataGridView is bound to the Text
property by default?

[quoted text, click to view]
* I'll do this as soon as I have some time. I understand that would be
optimal but I'm pressed for time right now. For now:
The DataSet is filled and the Columns are displayed correctly (the headers
show up, etc.). The data isn't being bound to the cells. I don't get
BindingComplete events or errors.

[quoted text, click to view]
* Thanks for the link to this excellent resource! I'm currently looking
through it and will reply again as soon as I can.

Thank you for your time,
jetan NO[at]SPAM online.microsoft.com (
5/21/2007 3:25:18 AM
Hi,

Thank you for the feedback.

There are 2 types of databinding in .Net Winform: simple databinding and
complex databinding. The simple databinding uses Binding class object as
the relation. It represents the binding between the property value of an
object and the property value of a control. So we must use it on the
control property, instead of 2 objects.

Complex databinding is used to bind a control to a collection of
datasource. DataGridView uses complex databinding instead of simple
databinding, so it does not use Binding object.

If you explicit added 2 DataGridViewTextBoxColumn without setting its
DataPropertyName property, it will not bind to any datacolumn. So these 2
columns will not display any data at all, there is no default setting. In
complex databinding, the control is responsible for display the datasource
data in the UI.

Ok, if you meet any further problem, please feel free to feedback, thanks

Best regards,
Jeffrey Tan
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.
AddThis Social Bookmark Button