Can you add data to a datagrid without a data source?

I have a DataGrid with 5 columns of templates,

However, when I try to add and add dynamically created controls to the grid, it fails because there are no rows.

- Can I add an empty string and use it? And How? -How else?

+3
source share
2 answers

I am sure you should bind to the data source. But it’s easy enough to create your own DataTableand insert a string with some fictitious information into it.

//pseudo code:

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("column1");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["column1"] = "value1";
dt.Rows.AddNew(dr);

myDataGrid.DataSource = dt;
myDataGrid.DataBind();
+6
source

DataGridView, , DataGridView. DataGrid, DataGridView.

// Sample code to add a new row to an unbound DataGridView
DataGridViewRow YourNewRow = new DataGridViewRow();

YourNewRow.CreateCells(YourDataGridView);
YourNewRow.Cells[0].Value = "Some value";
YourNewRow.Cells[1].Value = "Another value";

YourDataGridView.Rows.Add(YourNewRow);
+6

Source: https://habr.com/ru/post/1699225/


All Articles