如何解决以行编码方式添加到datagridview的错误 [英] How do I resolve the error that says rows cannont programatically added to the datagridview
本文介绍了如何解决以行编码方式添加到datagridview的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< pre> DataSet info1 = this .GetData.getInfo( SELECT 0 AS EmpNo,Er.EmpID,(Er.FName&''& Er.LName)AS [员工姓名],Ed.DeptNo AS [部门号], Er.Gender AS [Sex] FROM(EmpRegist AS Er INNER JOIN EmployeeDept AS Ed ON Er.EmpId = Ed.EmpId)WHERE Ed.DeptId = +( object )DpetID + AND Ed.Status = 0 AND Er.EmpStatus = 1 ORDER BY + str, EmployeeDept);
if (info1.Tables [ 0 ]。Rows.Count > 0 )
{
for ( int index = 0 ; index < info.Tables [ 0 ]。Rows.Count; ++ index)
// 错误出现在下面的代码
此 .dgvPrint.Rows.Add(( object ) 0 ,( object )(index + < span class =code-digit> 1 ),( object )info1.Tables [ 0 ]。行[index] [ 员工姓名]。T oString(),( object )info1.Tables [ 0 ]。行[index] [ 部门否]。ToString(),( object )info1.Tables [ 0 ]。行[index] [ EMPID跨度>]的ToString());
}
if ( this .dgvPrint.Rows.Count > 0 )
此 .btnPrint。已启用= true ;
else
this .btnPrint.Enabled = 假跨度>;
}
我的尝试:
我如何解决< blockquote class =quote>< div class =op>引用的错误:< / div>当控件是数据绑定时,无法以编程方式将行添加到DataGridView的行集合中< / blockquote>
解决方案
< blockquote>
Quote:当控件是数据绑定时,无法以编程方式将行添加到DataGridView的行集合中
我会说它很安静......因为你现在使用网格显示一些数据源(因此数据绑定),你不能直接改变它...
你必须将行添加到数据源并刷新显示...
<pre>DataSet info1 = this.GetData.getInfo("SELECT 0 AS EmpNo,Er.EmpID,(Er.FName&''&Er.LName) AS [Employee Name], Ed.DeptNo AS [Department No],Er.Gender AS [Sex] FROM (EmpRegist AS Er INNER JOIN EmployeeDept AS Ed ON Er.EmpId = Ed.EmpId) WHERE Ed.DeptId=" + (object)DpetID + " AND Ed.Status=0 AND Er.EmpStatus=1 ORDER BY " + str, "EmployeeDept");
if (info1.Tables[0].Rows.Count > 0)
{
for (int index = 0; index < info.Tables[0].Rows.Count; ++index)
//Error Appears on the code below
this.dgvPrint.Rows.Add((object)0, (object)(index + 1), (object)info1.Tables[0].Rows[index]["Employee Name"].ToString(), (object)info1.Tables[0].Rows[index]["Dept No"].ToString(), (object)info1.Tables[0].Rows[index]["EmpId"].ToString());
}
if (this.dgvPrint.Rows.Count > 0)
this.btnPrint.Enabled = true;
else
this.btnPrint.Enabled = false;
}
What I have tried:
How can i solve the error that says "<blockquote class="quote"><div class="op">Quote:</div>Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound</blockquote>"
解决方案
Quote:Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound
I would say it is quiet clear... As you grid now use to display some data source (hence data-bound), you can not alter it directly...
You have to add rows to the data source and refresh the display...
这篇关于如何解决以行编码方式添加到datagridview的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文