How to bind Dataset to DataGridView in windows application

前端 未结 3 770
南方客
南方客 2020-12-05 18:58

I have created Windows Application. In this, I have multiple tables in dataset, now I want to bind that to a single DataGridView. Can anybody help me?

3条回答
  •  自闭症患者
    2020-12-05 19:24

    following will show one table of dataset

    DataGridView1.AutoGenerateColumns = true;
    DataGridView1.DataSource = ds; // dataset
    DataGridView1.DataMember = "TableName"; // table name you need to show
    

    if you want to show multiple tables, you need to create one datatable or custom object collection out of all tables.

    if two tables with same table schema

    dtAll = dtOne.Copy(); // dtOne = ds.Tables[0]
    dtAll.Merge(dtTwo); // dtTwo = dtOne = ds.Tables[1]
    
    DataGridView1.AutoGenerateColumns = true;
    DataGridView1.DataSource = dtAll ; // datatable
    

    sample code to mode all tables

    DataTable dtAll = ds.Tables[0].Copy();
    for (var i = 1; i < ds.Tables.Count; i++)
    {
         dtAll.Merge(ds.Tables[i]);
    }
    DataGridView1.AutoGenerateColumns = true;
    DataGridView1.DataSource = dtAll ;
    

提交回复
热议问题