Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How do I get my DataTable to show up as a DataSource in windows form?
 

How do I get my DataTable to show up as a DataSource in windows form?

Hi all,

I've created a DataTable and populated it with some data. I've made it a bindingsource and bound it to a Zedgraph control programatically and this works fine. I did this via:

bindingSource1->DataSource = resultDataTable;
myZedGraph->DataSource = bindingSource1;

My problem occurs when trying to also bind this to a dataGridView.

When I click on the dataGridView in the form design and try to select a DataSource the resultDataTable I created does not show up there. I've tried rebuilding and still no table. Is it not showing up because I've already bound in programatically to a bindingSource for the ZedGraph control?

As always, any help/advice is much appreciated.

Thanks in advance,
Alex


Just_Alex  Thursday, October 01, 2009 6:27 PM
You can set thedata sourceof a binding source to an objectdata sourceof your desired type at design time. Then you can assign adata table(orpreferablya collection of business layer entity class) to the binding source at runtime.

The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Thursday, October 01, 2009 7:05 PM
Hi webJose,

Based on my understanding, you can bind the BindingSource to the DataGridView programmatically as follows:
     dataGridView1->DataSource = bindingSource1;

If you want to add the binding in designer. You can click the drop down button on the DataSource property editor of the DataGridView in the properties editor and follow the guide to add a data source.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 4:38 AM
Don't know of a guide, but I can quickly tell you that everything that you see in design-time is a component.  A component is any class that implements the IComponent interface.  From forms and controls to timers and datasets, all are components.  If you want to assign something to something in design-time, those must be components.  Important to note:  Anything showing in the designer is a component, but not every component can show in the designer.

Components behave differently in run-time than in design-time.  The design-time behavior is influenced considerably by a special class call the designer.  There are two major designer classes:  The control designer, and the component designer.  Of the two, the latter is the simplest.  Designers drive several design-time features, like smart tags.

But anyway, in your particular case, you want the Control DataGridView to be able to databind with a DataTable object.  It is possible, but not straightforward.  You need to start out with a DataSet or a BindingSource, or even a SqlDataAdapter.  DataSet and BindingSource are found in the toolbox; you need to manually add a SqlDataAdapter to the toolbox before adding it to a form.  I'd use a BindingSource, I think.

After adding a suitable data source from the toolbox, you configure it.  This configuration of the data source is one of the fundamental parts.  It will let the data source know where the data is (usually a data server).  The data source will then usually automatically generate a couple more components for you.  In the case of the BindingSource (if I remember correctly), it will generate a DataSet component that contains the DataTable component with the data.

It is at this point where you can select your DataGridView and look for the DataSource property in the Properties window.  Select the BindingSource, or select one of its children.  Now you are data bound.  Depending on your selection, you'll need to specify a DataMember, or you already see some of your data displayed, meaning the configuration is complete.

Hope this helps.
MCP
webJose  Wednesday, October 07, 2009 5:50 PM
You can set thedata sourceof a binding source to an objectdata sourceof your desired type at design time. Then you can assign adata table(orpreferablya collection of business layer entity class) to the binding source at runtime.

The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Thursday, October 01, 2009 7:05 PM
I'm not sure I follow.  The DataTable class is marked with ToolboxItemAttribute(false) and DesignTimeVisibleAttribute(false).  The former will prevent the class from appearing in the toolbox, and the latter will prevent instances of the class to visually show in the designer.

Since it is not in the toolbox, and cannot be seen visually in the designer, how are you adding the DataTable at design-time?
MCP
webJose  Friday, October 02, 2009 1:34 AM
Hi webJose,

Based on my understanding, you can bind the BindingSource to the DataGridView programmatically as follows:
     dataGridView1->DataSource = bindingSource1;

If you want to add the binding in designer. You can click the drop down button on the DataSource property editor of the DataGridView in the properties editor and follow the guide to add a data source.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 4:38 AM
I guess my biggest mistake here has been trying to add as a Data Source in the Designer, something I don't create until run time (programatically).

I've tried the method Aland Li has suggested in a separate test project using the Data Source Configuration Wizard to gain a bit of understanding about how data binding to a datagridview works but I'm still a bit confused. What I would like to do seems simple enough:

Place a datagridview on a form, bind it to a DataTable Object, populate the DataTable with some data and see the results show up in the datagridview. I can do this in the code but I'd like to learn how to do this using some/all of the power of the designer.

Does anyone know of a good guide, FAQ or ebook that explains how to do this?

Cheers,
Alex
Just_Alex  Wednesday, October 07, 2009 4:14 PM
Don't know of a guide, but I can quickly tell you that everything that you see in design-time is a component.  A component is any class that implements the IComponent interface.  From forms and controls to timers and datasets, all are components.  If you want to assign something to something in design-time, those must be components.  Important to note:  Anything showing in the designer is a component, but not every component can show in the designer.

Components behave differently in run-time than in design-time.  The design-time behavior is influenced considerably by a special class call the designer.  There are two major designer classes:  The control designer, and the component designer.  Of the two, the latter is the simplest.  Designers drive several design-time features, like smart tags.

But anyway, in your particular case, you want the Control DataGridView to be able to databind with a DataTable object.  It is possible, but not straightforward.  You need to start out with a DataSet or a BindingSource, or even a SqlDataAdapter.  DataSet and BindingSource are found in the toolbox; you need to manually add a SqlDataAdapter to the toolbox before adding it to a form.  I'd use a BindingSource, I think.

After adding a suitable data source from the toolbox, you configure it.  This configuration of the data source is one of the fundamental parts.  It will let the data source know where the data is (usually a data server).  The data source will then usually automatically generate a couple more components for you.  In the case of the BindingSource (if I remember correctly), it will generate a DataSet component that contains the DataTable component with the data.

It is at this point where you can select your DataGridView and look for the DataSource property in the Properties window.  Select the BindingSource, or select one of its children.  Now you are data bound.  Depending on your selection, you'll need to specify a DataMember, or you already see some of your data displayed, meaning the configuration is complete.

Hope this helps.
MCP
webJose  Wednesday, October 07, 2009 5:50 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView will not BeginEdit after BindingSource.Filter change
• DataGridViewCheckBoxColumn value not being set in data source
• Datagridview checkbox.checked event
• N:M relations and DataGridView
• Tree diagram and views--- Windows Application
• DataGridViewComboBoxColumn - populate existing combobox with items.add
• DBConcurrencyException
• Datagrid filter row selected.
• C# windows form application
• How to jump down to first letter you type in Datagridview?