简体   繁体   English

行不能以编程方式添加到datagrid

[英]Rows cannot be programmatically added to datagrid

How do I resolve this error: 我该如何解决此错误:

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound. 当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的rows集合中。

From the below code: 从下面的代码:

  string str = "Er.fName,Er.lName";
      DataSet dataSet = new DataSet();
      DataSet info = this.GetData.getInfo("SELECT 0 AS ErNo, Er.EmpID, (Er.fName&''&sr.lName) AS [Employee Name], Ed.DeptNo AS [Dept No] FROM (EmployeeReg AS Er INNER JOIN EmployeeDept AS Ed ON Er.EmpId = Er.EmpId)  WHERE Ed.DeptId=" + (object) DeptID + " AND  Ed.Status=0  AND Er.EmpStatus=1 ORDER BY " + str, "EmployeeDept");
if (info.Tables[0].Rows.Count > 0) {
    for (int index = 0; index < info.Tables[0].Rows.Count; ++index)
    //error from the code blw 
    this.dgvPrint.Rows.Add((object) 0, (object)(index + 1), (object) info.Tables[0].Rows[index]["Emp Name"].ToString(), (object) info.Tables[0].Rows[index]["Emp No"].ToString(), (object) info.Tables[0].Rows[index]["EmpId"].ToString());
}
if (this.dgvPrint.Rows.Count > 0) 
    this.btnPrint.Enabled = true;
else 
    this.btnPrint.Enabled = false;
var table = info.Tables[0];
table.Rows.Add(new object[] {
      0, 
      index + 1,
      table.Rows[index]["Emp Name"], 
      table.Rows[index]["Emp No"],
      table.Rows[index]["EmpId"]
});

or: 要么:

var table = info.Tables[0];
var new = table.NewRow();
new["colllName"] = 1;
new["colllName 2"] = 2;
//ect...
table.Rows.Add(new);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM