且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在datagridview中添加行

更新时间:2023-12-04 08:49:58

 有几种情况.我有两个想法,在这里讨论.如果您还有其他情况,请指定
//  1-如果您先创建一个dataTable,然后将bindingSource分配给GridView 
DataTable tbl =  DataTable(" ); // 创建表
tbl.columns.添加( DataColumn("  ColName")); // 创建列

DataRow dr = tbl.NewRow();
dr(" )= "  MyText";

DataGridView1.DataSource = tbl;

//  2-如果dataGrid是根据设计器形式设计的,并且包含1到n个单元格..

对象 [] arrObj =  对象 [ DataGridView1.Columns.Length]; // 获取列数
arrObj [ 0 ] = "  1 ] = " 跨度>;
.
.
.
obj [n] = " ;

DataGridView1.rows.添加(arrObj)


hi i created a column in datagridview like that

DataGridViewColumn newCol = new DataGridViewColumn(); // add a column to the grid

DataGridViewCell cell = new DataGridViewTextBoxCell(); //Specify which type of cell in 
newCol.CellTemplate = cell;
newCol.HeaderText = "Periods";
 newCol.Name = "Period";
 newCol.Visible = true;
newCol.Width = 40;



now i want to create rows for that column .if anyone knows about it help me.

//There are several scenarios. i had two in mind and discussed here. if you have some other scenario..please specify
//1- In case you first create a dataTable and then assign bindingSource to GridView
DataTable tbl = new DataTable("TableName"); //Create Table
tbl.columns.add(new DataColumn("ColName")); //Create Column

DataRow dr = tbl.NewRow();
dr("ColName") = "MyText";

DataGridView1.DataSource = tbl;

//2-if dataGrid is designed from designer form and contain 1 to n number of cells..

object[] arrObj = new object[DataGridView1.Columns.Length]; //GetNumber of columns
arrObj[0] = "Value for column1";
arrObj[1] = "Value for Column2";
.
.
.
obj[n] = "Value for Column n";

DataGridView1.rows.add(arrObj)