[英]how to bind datatable to datagridview in c#
我需要將我的DataTable綁定到我的DataGridView 。 我這樣做:
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;
但我得到的只是DataTable ADDS NEW列到我的DataGridView 。 我不需要這個,我只需要在現有列下寫。
試試這個:
ServersTable.Columns.Clear();
ServersTable.DataSource = SBind;
如果您不想清除所有現有列,則必須為每個現有列設置DataPropertyName
,如下所示:
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;
}
更好的是:
DataTable DTable = new DataTable();
BindingSource SBind = new BindingSource();
SBind.DataSource = DTable;
DataGridView ServersTable = new DataGridView();
ServersTable.AutoGenerateColumns = false;
ServersTable.DataSource = DTable;
ServersTable.DataSource = SBind;
ServersTable.Refresh();
您告訴可綁定源它綁定到 DataTable,反過來您需要告訴您的 DataGridView 不要自動生成列,因此它只會為您手動輸入到控件中的列提取數據。 .. 最后刷新控件以更新數據綁定。
在 DataGridView 上,將列的 DataPropertyName 設置為 DataTable 的列名。
// 我首先構建了我的數據表,並填充了它的列、行和所有內容。 //然后,一旦數據表起作用,請執行以下操作將其綁定到 DGV。 注意:對於此示例,DGV 的 AutoGenerateColumns 屬性必須為“true”,否則將無法將列名從數據表“分配”到 dgv。 我之前也將我的數據表“添加”到數據集,但我認為沒有必要。
BindingSource SBind = new BindingSource();
SBind.DataSource = dtSourceData;
ADGView1.AutoGenerateColumns = true; //must be "true" here
ADGView1.Columns.Clear();
ADGView1.DataSource = SBind;
//set DGV's column names and headings from the Datatable properties
for (int i = 0; i < ADGView1.Columns.Count; i++)
{
ADGView1.Columns[i].DataPropertyName = dtSourceData.Columns[i].ColumnName;
ADGView1.Columns[i].HeaderText = dtSourceData.Columns[i].Caption;
}
ADGView1.Enabled = true;
ADGView1.Refresh();
foreach (DictionaryEntry entry in Hashtable)
{
datagridviewTZ.Rows.Add(entry.Key.ToString(), entry.Value.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable StudentDataTable = new DataTable("Student");
//perform this on the Load Event of the form
private void AddColumns()
{
StudentDataTable.Columns.Add("First_Int_Column", typeof(int));
StudentDataTable.Columns.Add("Second_String_Column", typeof(String));
this.dataGridViewDisplay.DataSource = StudentDataTable;
}
}
//Save_Button_Event to save the form field to the table which is then bind to the TableGridView
private void SaveForm()
{
StudentDataTable.Rows.Add(new object[] { textBoxFirst.Text, textBoxSecond.Text});
dataGridViewDisplay.DataSource = StudentDataTable;
}
例如,我們希望通過以下 2 個步驟將 DataTable 'Users' 設置為 DataGridView:第 1 步 - 通過以下方式獲取所有用戶:
public DataTable getAllUsers()
{
OracleConnection Connection = new OracleConnection(stringConnection);
Connection.ConnectionString = stringConnection;
Connection.Open();
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("semect * from Users");
cmd.CommandType = CommandType.Text;
cmd.Connection = Connection;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}
return dataSet.Tables[0];
}
步驟 2- 將返回結果設置為 DataGridView :
public void setTableToDgv(DataGridView DGV, DataTable table)
{
DGV.DataSource = table;
}
使用示例:
setTableToDgv(dgv_client,getAllUsers());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.