DatagridView 添加与删除行

Rows cannot be programmatically added to the DataGridView's rows

当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。
 
关于这个问题的解决方法,网上只有以 dataTable / dataSet 做为数据源的解决办法。
一是,不绑定。二是,在 dataTable 上用 AddNew 。第三种是新插入一行,在绑定。
 
我现在是以 LINQ 为数据源来绑定 dataGridView 的。不绑定的话,要写很多代码,第三种没有测试,看描述,应该也是很麻烦。我想采取第二种,但是 LINQ 的结果不能直接转换成 DataTable 或 DataSet (请参见:http://msdn.microsoft.com/en-us/vbasic/bb688086.aspx)
 
一开始想用 BindingSource ,但是给BindingSource设完DataSource 之后,忘了给DataGridView 设定 DataSource 了,看不到结果,误以为用 BindingSource 不行。
 
搜不到解决办法,真的有点绝望的感觉。最后索性在试一把 BindingSource ,这下没有忘记给 DataGridView 设置 DataSource:
 
        private void SetGd2(int catID,string catName){
            gd2DefaultCatID = catID;
            gd2DefaultCatName = catName;
 
            gd2Bs = new BindingSource();
            gd2Bs.DataSource = dc.QV_Supplier_Category_Map.Where(m => m.CategoryID == catID);
            gd2.DataSource = gd2Bs;
        }
 
        private void gd2_DefaultValuesNeeded(object sender , DataGridViewRowEventArgs e) {
            e.Row.Cells["CategoryName"].Value = gd2DefaultCatName;
            e.Row.Cells["CategoryID"].Value = gd2DefaultCatID;
        }
 
        private void gd1_CellDoubleClick(object sender , DataGridViewCellEventArgs e) {
             QV_Supplier_Category_Map newRow = (QV_Supplier_Category_Map)gd2Bs.AddNew();
            newRow.CategoryID = gd2DefaultCatID;
            newRow.SupplierCategory = gd1.Rows[e.RowIndex].Cells["CategoryName"].Value.ToString();
            newRow.SupplierID = (int)gd1.Rows[e.RowIndex].Cells["SupplierID"].Value;
            gd2.CurrentRow.Cells["CategoryName"].Value = gd2DefaultCatName;
            gd2.CurrentRow.Cells["SupplierName"].Value = (string)gd1.Rows[e.RowIndex].Cells["SupplierName"].Value;
        }
 
可以以程序方式新增行了!但是却有另外一个问题, 用这种方法给 DataGridView 添加新行,DataGridView 的 DefaultValuesNeeded 这个事件没有触发。。。









本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/289948,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Winform中,可以通过以下步骤为DataGridView添加编辑和删除按钮: 1. 首先,确保已在窗体上添加DataGridView控件,并将其命名为dataGridView。 2. 添加编辑按钮: - 在窗体上添加Button控件,并设置其Text为“编辑”。 - 双击编辑按钮,进入按钮的点击事件处理方法。 - 在事件处理方法中,使用以下代码实现当用户点击编辑按钮时,获取选中的数据,并进编辑: ```csharp private void editButton_Click(object sender, EventArgs e) { if(dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; // 获取选中的数据 string data = selectedRow.Cells["ColumnName"].Value.ToString(); // 进编辑操作 // ... } } ``` 3. 添加删除按钮: - 在窗体上添加Button控件,并设置其Text为“删除”。 - 双击删除按钮,进入按钮的点击事件处理方法。 - 在事件处理方法中,使用以下代码实现当用户点击删除按钮时,删除选中的数据: ```csharp private void deleteButton_Click(object sender, EventArgs e) { if(dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; // 删除选中的数据 dataGridView.Rows.Remove(selectedRow); } } ``` 通过以上步骤,就可以为DataGridView添加编辑和删除按钮了。在实际操作中,您可以根据具体需求对代码进适当调整和完善。另外,还可以根据需要设置按钮的位置、样式等来符合设计要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值