how to bind datatable to datagridview in c#

前端 未结 7 645
面向向阳花
面向向阳花 2020-11-27 20:50

I need to bind my DataTable to my DataGridView. i do this:

        DTable = new DataTable();
        SBind = new BindingSou         


        
相关标签:
7条回答
  • 2020-11-27 21:47

    Try this:

        ServersTable.Columns.Clear();
        ServersTable.DataSource = SBind;
    

    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;
    }
    
    0 讨论(0)
提交回复
热议问题