I am working with a VB.Net winform and datagrid. I am dynamically creating the datagridview columns and setting the data property. Here is part of the code that creates the datagrid view.
MyDataGrid.ColumnCount = 13
MyDataGrid.Columns(0).Name = "MRN"
MyDataGrid.Columns(0).HeaderText = "MRN"
MyDataGrid.Columns(0).DataPropertyName = "med_rec_nbr"
MyDataGrid.Columns(1).Name = "LastName"
MyDataGrid.Columns(1).HeaderText = "Last Name"
MyDataGrid.Columns(1).DataPropertyName = "last_name"
MyDataGrid.Columns(2).Name = "FirstName"
MyDataGrid.Columns(2).HeaderText = "First Name"
MyDataGrid.Columns(2).DataPropertyName = "first_name"
MyDataGrid.Columns(3).Name = "DateOfBirth"
MyDataGrid.Columns(3).HeaderText = "Date of Birth"
MyDataGrid.Columns(3).DataPropertyName = "date_of_birth"
After setting the datasource to a datatable from SQL Server source, the columns change. Here is the code where I set the datasource.
Dim dtResults As DataTable
dtResults = GetSQLData(ID)
MyDataGrid.DataSource = dtResults
Here is a before picture.
Here is the after loading the datatable
The weird thing is, I have other DataGridViews and they are not behaving like this at all. Why are the columns changing? What should I look for? Is there a property i should look for?
CodePudding user response:
Actually you created dynamic columns are not reflect after bind the data from data table. What actually is , what order in datatable result, that order bind finally. Better in select query you change the order and which columns need and then bind in datagridview.And also use DataBindings.
MyDataGrid.DataSource = dtResults
MyDataGrid.DataBindings()
CodePudding user response:
MyDataGrid.AutoGenerateColumns = False