C#如何将datagridview的currentrow复制到单个datarow? [英] C# how to copy datagridview's currentrow to single datarow ?

查看:126
本文介绍了C#如何将datagridview的currentrow复制到单个datarow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 DataRow dr; 
dr =((dgv.DataSource) as DataTable)。行[dgv.CurrentRow.Index];





dgv是在winform上预定义的DataGridView。

dr总是单身。



我尝试了什么:



找到上面的代码。

但它不能正常工作。因为'CurrentRow.Index'没有与'DataTable的索引'相匹配。

我认为将DataGridViewRow复制到DataRow是最简单的方法。这是不可能的?



尝试这个的原因是当更改dgv时更新到/从数据库中删除。来自本地dgv的远程DB CRUD的任何其他更好的解决方案都应该很棒。



提前谢谢。

解决方案

< blockquote>使用CurrentRow项及它的DataBoundItem属性:对我来说,它返回一个DataRowView,它允许我访问Row本身:

 DataRowView drv = myDataGridView.CurrentRow.DataBoundItem as DataRowView ; 
if(drv!= null)
{
DataRow dr = drv.Row;
}


DataRow dr;
dr = ((dgv.DataSource) as DataTable).Rows[dgv.CurrentRow.Index];



dgv is DataGridView predefined on winform.
dr is single always.

What I have tried:

Above code was found.
But it doesn't work right. Because 'CurrentRow.Index' is not macthed to 'DataTable's Index'.
I think to copy DataGridViewRow to DataRow is simplest way. Is it impossible?

Reason trying this is for Update to/Delete from database when dgv is changed. Any other better solution for remote DB CRUD from local dgv should be great.

Thanks in advance.

解决方案

Use the CurrentRow item and it's DataBoundItem property: for me, it returns a DataRowView which lets me access the Row itself:

DataRowView drv = myDataGridView.CurrentRow.DataBoundItem as DataRowView;
if (drv != null)
    {
    DataRow dr = drv.Row;
    }


这篇关于C#如何将datagridview的currentrow复制到单个datarow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆