访问 GridView 中的项目
我有一个 GridView,我正在通过 C# 通过 LINQ 更新它。我想访问 GridView 中的元素并将它们存储在变量中。然后我将这些变量传递到我的 LINQ 查询中。
我应该如何访问 GridView 中的项目?这是我的代码:
protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string productID = gvShowComm.DataKeys[e.RowIndex]["Product_ID"].ToString(); //Working!
string planName = gvShowComm.DataKeys[e.RowIndex]["PlanName"].ToString(); //Working!
string hiComm = gvShowComm.DataKeys[e.RowIndex]["HiCommissionOld"].ToString(); //Working!
string lowComm = gvShowComm.DataKeys[e.RowIndex]["LowCommissionOld"].ToString(); //Working!
gvShowComm.DataBind();
}
标记:
<asp:GridView runat="server" Height="233px" Width="602px" ID ="gvShowComm"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing = "gvShowComm_RowEditing"
OnRowUpdating = "gvShowComm_RowUpdating"
OnRowCancelingEdit = "gvShowComm_RowCancelingEdit" DataKeyNames = "Product_ID, PlanName, HiCommissionOld, LowCommissionOld">
<Columns>
<asp:CommandField ShowCancelButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
I have a GridView and I am updating it through LINQ through C#. I want to access the elements in my GridView and store them in the variable. Then I will pass these variables into my LINQ query.
How should I access the items in my GridView? Here is my code:
protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string productID = gvShowComm.DataKeys[e.RowIndex]["Product_ID"].ToString(); //Working!
string planName = gvShowComm.DataKeys[e.RowIndex]["PlanName"].ToString(); //Working!
string hiComm = gvShowComm.DataKeys[e.RowIndex]["HiCommissionOld"].ToString(); //Working!
string lowComm = gvShowComm.DataKeys[e.RowIndex]["LowCommissionOld"].ToString(); //Working!
gvShowComm.DataBind();
}
Markup:
<asp:GridView runat="server" Height="233px" Width="602px" ID ="gvShowComm"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing = "gvShowComm_RowEditing"
OnRowUpdating = "gvShowComm_RowUpdating"
OnRowCancelingEdit = "gvShowComm_RowCancelingEdit" DataKeyNames = "Product_ID, PlanName, HiCommissionOld, LowCommissionOld">
<Columns>
<asp:CommandField ShowCancelButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像 Produce_ID 这样的东西听起来像是一个关键字段,如果是的话,您可以这样做
,否则
您可以使用
e.NewValues[]
和e.OldValues[]
。Something like Produce_ID sounds like a key field and if it is, you can do
or else
there is
e.NewValues[]
ande.OldValues[]
that you could use.GridViewUpdateEventArgs 具有以下属性:允许您访问 键,旧值和NewValues 特定行的集合。
您可以像这样访问它:
但这很难! ASP.Net 可以使用可用的 DataSource 类之一为您完成这项工作。出于您的目的,您应该查看 LinqDataSource
值得注意的是,虽然DataSource 类非常有用且美观,它们具有相当多的魔力,并且您几乎可以放弃使用任何可测试模式的可能性。 (请参阅WebFormsMVP)
如果这不是问题,那就去吧。但从长远来看,实现关注点的彻底分离会更好。在这种情况下,您需要围绕业务逻辑实现外观包装器并使用 ObjectDataSource 。
无论哪种方式,您都比手动在键/值对集合中查找值然后将它们转换回正确的类型更好。
The GridViewUpdateEventArgs has properties on it that allow you to get at the Keys, OldValues, and NewValues collections for a particular row.
You could access it like so:
But this sucks pretty hard! ASP.Net can do the work for you, using one of the DataSource classes available. For your purposes you should look into the LinqDataSource
It is worth noting that while the DataSource classes can be useful and nice, they have quite a bit of magic going on, and you can pretty much throw out the possibility of using any testable patterns with them. (see WebFormsMVP)
If that isn't a concern, then go for it. But achieving clean separation of concerns is better in the long run. In that case you would need to implement a facade wrapper around your business logic and use the ObjectDataSource.
Either way you are better off than manually rooting around in key/value pair collections looking for values and then converting them back to their proper types.