如何在datagridview中添加绑定到数据源的新行 [英] How to add New Row in datagridview which is bound to the datasource

查看:233
本文介绍了如何在datagridview中添加绑定到数据源的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview绑定到一个数据源。当我点击编辑按钮或新建按钮时,我必须在datagridview中添加一行。我尝试了一些代码,但是给我错误,代码如下:

  DataGridView grdview = new DataGridView(); 
grdview.Rows.Add();
grdview.Rows [grdview.Rows.Count - 1] .Cells [0] .Selected = true;
grdview.BeginEdit(false);

我还尝试将数据源类型转换为datatable,但没有解决方案。

解决方案

不,如果 DataGridView 是数据绑定到一些数据源,那么您不能添加新的行/列。您将必须使用所需的列创建一个新的数据集( DataTable 或其他),然后重新绑定 DataGridView

我希望这有帮助。


I have a datagridview that is bound to a datasource. I have to add a new row in datagridview when I click Edit button or New button.I tried some code but its giving me error, code is given below

DataGridView grdview = new DataGridView();
grdview.Rows.Add();
grdview.Rows[grdview.Rows.Count - 1].Cells[0].Selected = true;
grdview.BeginEdit(false);

I also tried typecasting of datasource to datatable but no solution.

解决方案

No, if the DataGridView is data bound to some data source then you cannot add new rows/columns. You will have to create a new data set (DataTable or whatever) with the required columns and then re-bind the DataGridView to that.

I hope this helps.

这篇关于如何在datagridview中添加绑定到数据源的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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