Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a window form datagridview having combobox and textbox elements. I used this datagridview to accept data from user then saved in sql database.

Whenever i attempted to populate datagridview control with same data from sql it always create new sets of controls on same datagridview to show my sql data whereas it leaves those controls i created in design view empty.

What do I do to populate my sql data on existing control on the same datagridview object.

What I have tried:

Dim str As String
str = "select * from ifmis_staff_matrix k order by gl desc"
cn.Open()
Dim com As New SqlCommand(str, cn)
Dim rd As SqlDataReader

rd = com.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As DataTable = New DataTable()
dt.Load(rd)

DataGridView3.DataSource = dt
Posted
Updated 21-Mar-18 2:13am

1 solution

Try to change DataGridView.AutoGenerateColumns Property (System.Windows.Forms)[^] to false to stop generating new columns. But..
MSDN wrote:
If you set the DataSource property but set AutoGenerateColumns to false, you must add columns manually. You can bind each added column to the data source by setting the DataGridViewColumn.DataPropertyName property to the name of a property exposed by the bound objects.
 
Share this answer
 
v2
Comments
Olaoluwa 21-Mar-18 12:43pm    
Do you mean I can continue the code like this:

with DataGridView3
.DataSource = dt
.DataGridViewColumnName.DataPropertyName="category"
end with

and it will produce the kind of result i wanted.
Maciej Los 21-Mar-18 12:56pm    
No. If you've defined set of DataGridView's columns manually, you can set only: DataGridView3.DataSource = dt, but before that you have to set AutoGenerateColumns property to false (either manually or in code!).
BTW: Thank you for down-voting.
Olaoluwa 22-Mar-18 5:08am    
You are my hero man. I tested it overnight and it worked. Thumb up
Maciej Los 22-Mar-18 5:57am    
Nice to hear that.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900