Search code examples
c#datagridviewdatasource

How to bind DataTable to DataGridView as a DataSource


I have a DataTable and I insert a Row using DataColumnCollection, and then bind to the DataSource. Afterwards, I saw in the grid's first column System.Data.DataColumnCollection that the value is not present. Give me an idea.


Solution

  • Try it,

    DataTable dt=new DataTable();
    dt.Columns.Add("No",typeof(int)); // Adding column into ColumnsCollection
    dt.Columns.Add("Name");
    
    dt.Rows.Add(1,"AAA");  // Adding rows into RowsCollection
    dt.Rows.Add(2,"BBB");
    
    DataGridView1.DataSource=dt; // Binding datasource