Linking a DataGridView directly to a DataTable

Never use this because it is a nightmare if you want to create a bespoke interface.

Rows cannot be added to a DataGridView programmatically when the control is data bound.
This can be done using the DataSource property of the DataGridView control.
The column headings will be automatically taken from the headings in the DataTable
After the DataGridView has been populated you need to then define the widths of the columns

When you bind a DataGridView control you can use the "AutoGenerateColumns" property to automatically generate default column types appropriate for the data types in the bound data sources.

Call clsDatabase.ConnectionOpen(gsConnectionString, gsSOLUTION_NAME) 

clsDatabase.mobjDataSet = New System.Data.DataSet()

clsDatabase.gDataAdapter = New System.Data.OleDb.OleDbDataAdapter()
clsDatabase.gDataAdapter.TableMappings.Add("Table", "TableName")

clsDatabase.gsSQLQuery = ""
clsDatabase.gsSQLQuery = clsDatabase.gsSQLQuery & " SELECT"
clsDatabase.gsSQLQuery = clsDatabase.gsSQLQuery & " Columns
clsDatabase.gsSQLQuery = clsDatabase.gsSQLQuery & " FROM"
clsDatabase.gsSQLQuery = clsDatabase.gsSQLQuery & " TableName

objdatacommand.Connection = clsDatabase.gDataConnection
objdatacommand.CommandText = clsDatabase.gsSQLQuery

clsDatabase.gDataAdapter.SelectCommand = objdatacommand
irowsreturned = clsDatabase.gDataAdapter.Fill(clsDatabase.mobjDataSet, "TableName")

dgvDataGridView.DataSource = clsDatabase.mobjDataSet.Tables("TableName").DefaultView
dgvDataGridView.Columns(0).Width = 50

Call clsDatabase.ConnectionClose(gsSOLUTION_NAME)

The dataset object in your program is only a representation of the data in your original database. If the user makes any changes to this data, it is not written back to the original database unless you specifically instruct the data adapter object to make these changes

When you make a change to the data grid, the data grid automatically updates the dataset to which the data grid is bound.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext