I need to bind my DataTable to my DataGridView. i do this:
DTable = new DataTable();
SBind = new BindingSource();
//ServersTable - DataGridView
for (int i = 0; i < ServersTable.ColumnCount; ++i)
{
DTable.Columns.Add(new DataColumn(ServersTable.Columns[i].Name));
}
for (int i = 0; i < Apps.Count; ++i)
{
DataRow r = DTable.NewRow();
r.BeginEdit();
foreach (DataColumn c in DTable.Columns)
{
r[c.ColumnName] = //writing values
}
r.EndEdit();
DTable.Rows.Add(r);
}
SBind.DataSource = DTable;
ServersTable.DataSource = SBind;
But all i got is DataTable ADDS NEW columns to my DataGridView. I don't need this, i just need to write under existing columns. please, help me, guys!
Even better:
You're telling the bindable source that it's bound to the DataTable, in-turn you need to tell your DataGridView not to auto-generate columns, so it will only pull the data in for the columns you've manually input into the control... lastly refresh the control to update the databind.
Try this:
If you don't want to clear all the existing columns, you have to set
DataPropertyName
for each existing column like this:for example we want to set a DataTable 'Users' to DataGridView by followig 2 steps : step 1 - get all Users by :
step 2- set the return result to DataGridView :
using example:
On the DataGridView, set the DataPropertyName of the columns to your column names of your DataTable.