Using DataGrid Without DataSet (unbound)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to use a DataGrid and manually populate the data in the table. Does
anyone know how this is done?? I want to create all the Columns, and then
just add rows as my Program runs. Is there something else I should be using
in C# than a DataGrid.

Thanks for the Help
 
Wes,

Why not use the DataSet? You can do exactly what you want in code, just
add the columns, and then add rows as needed. Then just bind to the data
set, and the grid will do the rest. A DataSet does not require an
underlying data source in order to be used. It is an in-memory data
structure.

Hope this helps.
 
Add a DataSet from the toolbox to your form. Using the Properties Window, add
a DataTable to the dataSet and add Columns to the DataTable. Or create your
own table at runtime:

dataTable1 = new DataTable("tablename");
dataTable1.Columns.Add("column 1", typeof(String));
dataTable1.Columns.Add("column 2", typeof(String));
dataTable1.Columns.Add("column 3", typeof(int));


To populate your DataTable:

DataRow dataRow = dataTable1.NewRow();
dataRow[ "column 1" ] = "value 1";
dataRow[ "column 2" ] = "value 2";
dataRow[ "column 3" ] = 3;
dataTable1.Rows.Add(dataRow);

or

dataTable1.Rows.Add(new object[]{ "value 1", "value 2", 3 });


Finally, bind your DataTable to your DataGrid:

dataGrid1.SetDataBinding(dataTable1, "");


Best Regards,
Phil.
 
Back
Top