如何绑定到数据表中的datagridview C# [英] how to bind datatable to datagridview in c#
本文介绍了如何绑定到数据表中的datagridview C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要绑定我的数据表我的的DataGridView 。 我这样做:
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;
不过,我的一切是数据表 增加了新的的列到我的的DataGridView 。 我不需要这个,我只需要在现有列写。 请帮帮我,伙计们!
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!
推荐答案
试试这个:
ServersTable.Columns.Clear();
ServersTable.DataSource = SBind;
如果您不想清除所有现有列,你必须设置 DataPropertyName
对于这样每个现有列:
If you don't want to clear all the existing columns, you have to set DataPropertyName
for each existing column like this:
for (int i = 0; i < ServersTable.ColumnCount; ++i) {
DTable.Columns.Add(new DataColumn(ServersTable.Columns[i].Name));
ServersTable.Columns[i].DataPropertyName = ServersTable.Columns[i].Name;
}
这篇关于如何绑定到数据表中的datagridview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文