如何编辑与集合绑定的DataGridView [英] how to edit a DataGridView which is bound with a Collection

查看:94
本文介绍了如何编辑与集合绑定的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我有一个与业务对象集合绑定的DataGridView.网格填充订单明细(OrderDetailId,ItemCode,ItemName,Qty,Rate,Amount ...)数据.

现在,我想通过网格编辑项目行.这个想法是,当我单击网格中的itemrow时,它应该将网格中的行数据填充到网格上方的各个控件中(假设Itemcode文本框中的Itemcode单元格值...).在对行数据进行必要的更改后,用户只需单击更新"按钮,它甚至在业务对象集合中也将更新Grid数据.

您能否帮我实现此目的的方法是什么?您有手头的例子吗,或者只是给我逻辑概述.

感谢您的宝贵努力.

Hi Friends,

I have a DataGridView which is bound with the Business object Collection. The Grid populates the Order detail(OrderDetailId,ItemCode, ItemName,Qty,Rate,Amount...) data.

Now I want to edit the item row through grid. The idea is when I click on itemrow in the grid it should fill the row data in the respective controls placed just above the grid(suppose Itemcode cell value in the Itemcode textbox...). after making the necessary changes in the row data user just click on update button and it updates the Grid data even in the Business object collection.

could you please help me what is way to implement this? Do you have any in hand example or you can just give me outline of the logic.

I will appreciate your valuable efforts.

Thanks

推荐答案

为什么没有尝试使用DataBoundItem属性访问绑定到DataGridViewRow 的集合项目.之后将其强制转换为项目类型并最后进行编辑
Why haven''t you tried accessing collection item bounded to DataGridViewRow using DataBoundItem property.Afterward cast it to the items type and and last edit it and refresh the bindings if needed.


这里是逻辑的一般概念.

1.当用户单击网格时(提示:当用户只是四处寻找"时,请使用双击以避免意外触发),获取所选的DataGridViewRow,保存对关键数据片段的引用(身份证号码或类似编号),然后将该行传递给将要执行以下操作的方法:
2.逐一查看编辑控件,例如myNameTextBox.Text = row.Cells["Name"].Value.ToString()

3.允许用户进行编辑.
4.当他们单击更新"按钮时,请使用先前保存的密钥在您的收藏夹中找到记录并进行更新,例如myBusinessObject.Name = myNameTextBox.Text.

接下来的操作将取决于您的集合如何绑定到您的DataGrid.如果您使用的是BindingSource(推荐),则网格应该会自动更新,否则您将不得不重新绑定以反映所做的更改.

希望这可以帮助. :)
Here is a general idea of the logic.

1. when the user clicks the grid (tip: use double-click to avoid accidental triggering when user is just ''looking round'') get the DataGridViewRow that was selected, save a reference to a key piece of the data (an identity number or similar), and then pass the row to a method that will:
2. Go through your editing controls one-by-one retrieving the data e.g. myNameTextBox.Text = row.Cells["Name"].Value.ToString()

3. Allow user to edit.
4. When they click the ''Update'' button use the key saved earlier to locate the record in your collection and update it e.g. myBusinessObject.Name = myNameTextBox.Text.

What you do next will depend on how your collection is bound to your DataGrid. If you have used a BindingSource (recommended) the grid should update automatically otherwise you will have to re-bind to reflect the changes.

Hope this helps. :)


非常感谢Henry(
)
现在我有两种方法受到关注,以实现这一目标.

1)要么通过当前行的DataBoundItem属性检索对象,然后通过编辑控件更新对象,然后它将在集合以及Grid中进行更新.

2)其次,按照您的说明,我使用Uniquekey更新对象.

但是我想知道当网格具有大量记录时,哪一个执行效果最好,以及在用户更新记录时在实体类中实现的繁重业务验证.

我认为两者都很完美:)
Thank you very much Henry

now I have two approaches receiving attention to implement this.

1) Either I retrieve the object through DataBoundItem property for the current row and update the object through the editing control thereafter it will be updated back in the collection as well as in the Grid.

2) Secondly I use the Uniquekey to update the object as you explained.

But I want to know which one perform best when the grid is having a number of records and also heavy business validations implemented in the entity class while user is updating the records.

I think both are perfect :)


这篇关于如何编辑与集合绑定的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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