Question Ways to populate a datagridview

motorola

New member
Joined
Oct 11, 2011
Messages
3
Programming Experience
5-10
This code works normal for normal situation:
VB.NET:
DataGridView1.DataSource = Datatable1

The problem with this code is that I must arrange the order of the fields in the select statement. How do I add rows to the datagridview manually and preset the order of the columns by myself?
The reason I want to do this is because I want to add buttons and dtPicker within the datagridview, and I cannot see how I can achieve this using the snippet above. And that creates a second question : how do I embed button and dtPicker to a datagridview?

Thanks
 
You can add columns to the grid in the designer. There are several predefined column types, including one for Buttons. There is not one for DateTimePickers though. One option is to create your own custom column type, which Microsoft has already kindly done for you (although it could use a bit of improvement).

How to: Host Controls in Windows Forms DataGridView Cells

You can add bound or unbound columns in the designer. Adding bound columns will fix your column order issue. You can add a column and set its DataPropertyName property. When you bind the grid, i.e. set its DataSource, the grid column will then display data from the data source column or property with the specified name. Alterantively, you can simply bind the grid and let it create the columns, then set the DisplayIndex of each one to show them in the order you want.
 
How do I add bound columns? I tried accessing datasource property of the grid via properties window and chose Add New Project Data Source. I'm having trouble connecting to the MySQL Database. This connection string worked in my code : "Server=localhost;Port=3306;Database=test;Uid=root;Pwd=kxtg2431;Encrypt=true;"
but won't run during the datagrid datasource configuration. I've also added the mysql connector in to my project. Help? Thanks
 
Back
Top