将Datagrid复制到Datatable [英] Copy Datagrid to Datatable
本文介绍了将Datagrid复制到Datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将数据从datagrid复制到datatable.我需要从datagrid删除选定的行,为此,我正在尝试将数据从datagrid复制到datatable并从datatable删除选定的行并将数据表再次绑定到datagrid.但是我在if(dt.rows.count)行上收到对象引用错误.这可能是问题,还是有其他简便的方法来删除所选行.
How to copy data from datagrid to datatable. I need to delete the selected row from datagrid, for this i am trying to copy data from datagrid to datatable and deleting the selected row from datatable and bind the datatable again to datagrid. but i am getting the object reference error on the line if(dt.rows.count). what could be the problem or is there any other easy way to remove the selected row.
dt = (DataTable)Datagrid1.DataSource;
if (dt.Rows.Count > 0)
{
dt.Rows[2].Delete();
}
推荐答案
在此
dt = (DataTable)Datagrid1.DataSource;
之后,实际上dt为null
这就是为什么您会收到此错误的原因.
Thats why you are getting this error.
签出以下链接.
http://social.msdn.microsoft.com/论坛/en-US/winformsdatacontrols/thread/73c01abf-8c79-4ac8-9391-c8700cb1c917 [
checkout the follwing link.
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/73c01abf-8c79-4ac8-9391-c8700cb1c917[^]
Hope this may help.
请参阅此内容,可能会有所帮助
http://social.msdn.microsoft.com/论坛/en-US/csharpgeneral/thread/53983755-bcdd-4d35-b307-08b370e42f81 [
Refer this, might help
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/53983755-bcdd-4d35-b307-08b370e42f81[^]
这篇关于将Datagrid复制到Datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文