在没有 DataBind 的情况下向 GridView 添加新行 [英] Add New Row to GridView without DataBind

查看:28
本文介绍了在没有 DataBind 的情况下向 GridView 添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView,它允许随时编辑每一列、每一行中的值.用户输入他们的所有更改,单击保存"一次并提交所有更改.

I have a GridView that allows editing the values in every column, in every row, all the time. The user enters in all their changes, clicks Save once and all changes are commited.

用户还必须能够单击新建"按钮,在 GridView 中出现一个新行(是的,它必须显示在实际的 GridView 中),输入他们想要的任何数据,单击保存"并进行所有更改到数据库.

The user must also be able to click the New button, have a new row appear in the GridView (yep, it has to show up in the actual GridView), enter whatever data they want, click Save and have all changes go to the database.

现在.这是抛出我的用户案例:用户到达页面,对几个现有行进行了几次更改,然后需要添加新行,在新行中输入数据,单击保存,并将所有更改转到数据库.

Now. Here's the user case that's throwing me: a user arrives at the page, makes several changes on several existing rows, and then needs to add a new row, enter data in the new row, click Save, and have all changes go to the database.

但是,我见过的添加新的空行的唯一方法涉及重新绑定 GridView,这意味着它们的所有更改都将丢失.这显然不好.

However, the only ways I've seen to add a new, empty row involve rebinding the GridView, which means all of their changes will be lost. This is obviously no good.

所以,我的问题是:有哪些方法可以在不重新绑定 GridView 的情况下向 GridView 添加新的、空的、可编辑的行?

So, my question would be: what are some approaches to adding a new, empty, editable row to a GridView without having to rebind the GridView?

我唯一能想到的是,在新建按钮的点击事件中,从 GridView 中提取所有数据(包括用户的潜在编辑),将其保存到 ViewState(或其他),添加新行,重新填充网格.对我来说,这似乎有点 hacky,但它应该允许我在 GridView 上关闭 ViewState.

The only thing I can think of would be, on the New buttons' click event, suck all the data out of the GridView (including the user's potential edits), save it to ViewState (or whatever), add the new row, repopulate the grid. This, to me, seems a little hacky, but it should allow me to turn ViewState off on the GridView.

有什么想法吗?

推荐答案

我突然想到了两个选项.第一个是缓存您绑定到网格的原始结果,当您需要添加另一行时,您将数据行添加到要绑定到的数据表中,然后将其绑定到网格.如果网格中有更改,则需要更新数据表.完成所有更改并且用户单击保存按钮后,您可以遍历表并使用数据更新数据库.

Just off my head I can think of two options. The first is to cache the original results that you are binding into the grid and when you need to add another row you add a datarow to the datatabale that you are binding to and then bind this to the grid. If there are changes in the grid then you need to update the datatable. Once all changes have been made and the user clicks the save button you can iterate through the table and update the DB with the data.

可能看起来像这样

页面加载

  • 获取数据库数据并放入表中
  • 将表格绑定到网格
  • 将表存储在缓存中

当用户请求新行时

  • 获取缓存的数据对象.
  • 更新任何已更改的行
  • 添加一个空行绑定到网格

当用户保存网格时

  • 获取缓存的对象.
  • 进行最后一组更新
  • 遍历行并更新数据库

另一种方法是动态创建网格,但这将比您所描述的内容所付出的努力要多得多.

The other way to do this is by creating the grid dynamically but this will involve far more effort than it's worth given what you have described.

这篇关于在没有 DataBind 的情况下向 GridView 添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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