The DataGrid and DataTable structures are identical. If you try to write like this:
DataGridVeiw.DataSource = DataTable; // Pseudocode
In the DatagridView on the right, the DataTable columns are “glued”.
Do not recommend filling with a loop. Explain what is the reason? How to correctly and quickly populate the Datagrid from the DataTable?
Answer 1, authority 100%
// #r "System.Windows.Forms"
using System.Windows.Forms;
using System.Data;
var t = new DataTable ();
t.Columns.Add ("c1", typeof (int));
t.Rows.Add (1);
t.Rows.Add (2);
var f = new Form ();
var g = new DataGridView () {
Parent = f,
Dock = DockStyle.Fill,
AutoGenerateColumns = false
};
g.Columns.Add (
new DataGridViewTextBoxColumn () {
DataPropertyName = "c1", // binding a grid column to a DataColumn
HeaderText = "From 1"
});
g.DataSource = t;
f.ShowDialog ();
Answer 2, authority 100%
Your “pseudocode” is correct in principle.
Just when filling the DataTable, give the columns normal names (if you fill in from the database, then add AS ‘ColumnName’ to the query …) for example:
SELECT somename AS 'Name' FROM sometable;
Then:
using (MySqlConnection con = new MySqlConnection ())
{
con.ConnectionString = MSB.ConnectionString;
MySqlCommand com = new MySqlCommand (Query, con);
DataTable DT = new DataTable ();
try
{
MySqlDataAdapter adr = new MySqlDataAdapter (Query, con);
adr.SelectCommand.CommandType = CommandType.Text;
adr.Fill (DT);
return DT;
}
catch (Exception e)
{
Error = e.Message;
return null;
}
}
and then assign DataTable as source of DataGridVeiw
DataGridVeiw.DataSource = DataTable;
DataGridVeiw.Update ();
You don’t need to create columns in the DataGridVeiw itself.
Answer 3
A Stack user made a correct recommendation. I was looking for solutions myself, but it turned out to be very simple. For each DGV field, you need to set the DataPropertyName parameter equal to the name of the column from the DataTable that you bind to the DataSource. Everything.
Answer 4
I had the same problem but with a DataSet.
My DataGridView was filled with a button click, the first call it filled normally, the next time it was the same problem as yours, I solved it by clearing the DataSet before filling it.
public static void ConAndSortDGV (string connectionString, string sql, DataSet ds, DataGridView dataGridView)
{
try
{
SqlConnection connection = new SqlConnection (connectionString);
connection.Open ();
SqlDataAdapter adapter = new SqlDataAdapter (sql, connection);
adapter.Fill (ds);
dataGridView.DataSource = ds.Tables [0];
adapter.Dispose ();
connection.Close ();
for (int i = 0; i & lt; dataGridView.Columns.Count; i ++)
{
dataGridView.Columns [i] .SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
catch (SqlException ex)
{
MessageBox.Show (ex.Message);
}
}
private void button3_Click (object sender, EventArgs e) // FLOAT number
{
ds.Clear (); // required for correct zeroing of the table when entering data
string sql = $ "{Properties.Settings.Default.sqlReq1} {comboBox1.Text}";
ConAndSortDGV (connectionString, sql, ds, dataGridView1);
}
Answer 5
Faced this question. Solution:
grid.AutoGenerateColumns = false;
grid.DataSource = bindingSource;
grid.Columns ["Column name in Grid"]. DataPropertyName = "Column name in data source";