VS2010 - WinForms - DataGridView - Binding to DataSet

后端 未结 4 998
旧巷少年郎
旧巷少年郎 2021-01-13 05:56

I am new to winforms and I have a datagridview inside a table control. I am trying to bind it to display data.

DataSet dataSet = new DataSet();
DataTable dat         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 06:42

    As of note, the link BWC's answer gives off incorrect syntax for referencing a datatable from a dataset. You use []'s not ()'s to reference the index of datatables in a DS.

    DataSet dataSet = new DataSet();
    DataTable dataTable = dataSet.Tables.Add("Results");
    
    dataTable.Columns.Add("ID");
    dataTable.Columns.Add("Name");
    
    dataTable.Rows.Add("1","Jack");
    dataTable.Rows.Add("2","Donna");
    
    dataGridView1.DataSource = dataSet.Tables["dataTable"]
    

    If you are looking at checkboxes, but not one that is boundto any data, use the property editor of the DataGridView to edit the columns (click on the elipses "..." in that field)

    Click on Add, select unbound column, and finally choose the checkbox column type:

    enter image description here

提交回复
热议问题