how to bind datatable to datagridview in c#

前端 未结 7 690
面向向阳花
面向向阳花 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:23

    Even better:

    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();
    

    You're telling the bindable source that it's bound to the DataTable, in-turn you need to tell your DataGridView not to auto-generate columns, so it will only pull the data in for the columns you've manually input into the control... lastly refresh the control to update the databind.

提交回复
热议问题