【问题标题】:how to bind datatable to datagridview in c#如何在c#中将数据表绑定到datagridview
【发布时间】:2013-10-01 11:09:49
【问题描述】:

我需要将我的 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 向我的 DataGridView 添加新的 列。 我不需要这个,我只需要写在现有的列下。

【问题讨论】:

  • 您没有任何DataSet,您只有一个BindingSource 和一个DataTable。你的BindingSource 是空白的。

标签: c# .net winforms data-binding datagridview


【解决方案1】:

试试这个:

    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;
}

【讨论】:

    【解决方案2】:

    更好:

    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 不知道需要填充哪些列。
    • 您必须表达 SBind.DataMember,通常是行的 ID。您需要为每一列设置 DataPropertyName,例如:stackoverflow.com/questions/9154130/…
    • 是的,在 DataGridView 上您必须设置 DataPropertyName。在那之后,这个解决方案就像一个魅力。非常感谢。
    【解决方案3】:

    在 DataGridView 上,将列的 DataPropertyName 设置为 DataTable 的列名。

    【讨论】:

      【解决方案4】:

      // 我首先构建了我的数据表,然后填充了它的列、行和所有内容。 //然后,一旦数据表功能正常,请执行以下操作将其绑定到 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();
      

      【讨论】:

        【解决方案5】:
        foreach (DictionaryEntry entry in Hashtable)
        {
            datagridviewTZ.Rows.Add(entry.Key.ToString(), entry.Value.ToString());
        }
        

        【讨论】:

          【解决方案6】:
          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;
                  }
          

          【讨论】:

            【解决方案7】:

            例如,我们想通过以下 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());
            

            【讨论】:

              猜你喜欢
              • 2013-02-06
              • 1970-01-01
              • 2011-04-02
              • 1970-01-01
              • 1970-01-01
              • 2015-12-02
              • 2011-03-12
              • 2016-11-19
              • 1970-01-01
              相关资源
              最近更新 更多