VS2010 - WinForms - DataGridView - Binding to DataSet

后端 未结 4 997
旧巷少年郎
旧巷少年郎 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:39

    Setting DataSource property will bind datasouce. ASP.NET needs a .DataBind call instead.

    For the checkbox you need to add a column of type DataGridViewCheckBoxColumn.

    Best resource i found on databind (these are for framework 2.0/VS2005):

    • http://windowsclient.net/Samples/Go%20To%20Market/Data%20Binding/DataBinding%20FAQ.doc

    • http://windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc

    0 讨论(0)
  • 2021-01-13 06:40

    you only have to add this:

    dataGridView1.DataMember = ds.Tables(0).ToString()

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-13 06:54

    http://hodentekhelp.blogspot.com/2008/07/how-to-bind-dataset-to-datagridview.html

    This should help with your databinding

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn.aspx

    take a look at that for the checkbox column

    Here is some sample code

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("Blah",typeof(bool));
            dt.Columns.Add("Blah2");
            ds.Tables.Add(dt);
            dataGridView1.DataSource = ds.Tables[0];     
    
    0 讨论(0)
提交回复
热议问题