Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中将datatable绑定到datagridview#_C#_.net_Winforms_Data Binding_Datagridview - Fatal编程技术网

C# 如何在c中将datatable绑定到datagridview#

C# 如何在c中将datatable绑定到datagridview#,c#,.net,winforms,data-binding,datagridview,C#,.net,Winforms,Data Binding,Datagridview,我需要将我的数据表绑定到我的数据网格视图。 我这样做: DTable = new DataTable(); SBind = new BindingSource(); //ServersTable - DataGridView for (int i = 0; i < ServersTable.ColumnCount; ++i) { DTable.Columns.Add(new DataCo

我需要将我的数据表绑定到我的数据网格视图。 我这样做:

        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;
DTable=newdatatable();
SBind=newbindingsource();
//ServersTable-DataGridView
对于(int i=0;i
但我得到的只是DataTable向我的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;
}
for(int i=0;i
更好的是:

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的列名。

//我首先构建了我的DataTable,并填充了它、列、行和所有内容。
foreach (DictionaryEntry entry in Hashtable)
{
    datagridviewTZ.Rows.Add(entry.Key.ToString(), entry.Value.ToString());
}
//然后,一旦datatable正常工作,请执行以下操作将其绑定到DGV。注意:在本例中,DGV的AutoGenerateColumns属性必须为“true”,否则将列名从datatable“赋值”到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();
BindingSource SBind=new BindingSource();
SBind.DataSource=dtSourceData;
ADGView1.AutoGenerateColumns=true//这里一定是“真的”
ADGView1.Columns.Clear();
ADGView1.DataSource=SBind;
//从Datatable属性设置DGV的列名和标题
对于(int i=0;i
例如,我们希望通过以下两个步骤将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());

您没有任何
数据集
,只有一个
BindingSource
和一个
DataTable
。您的
BindingSource
为空。看起来您是对的,但现在它只添加了新的空行,没有填充任何单元格。看起来datagridview不知道需要填充哪些列。您必须表示SBind.DataMember,通常是行的ID。您需要为每个列设置DataPropertyName,例如:是的,在DataGridView上必须设置DataPropertyName。在那之后,这个解决方案就像一个符咒。谢谢。
    setTableToDgv(dgv_client,getAllUsers());