访问 GridView 中的项目

发布于 2024-10-31 14:37:22 字数 1184 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2024-11-07 14:37:22

像 Produce_ID 这样的东西听起来像是一个关键字段,如果是的话,您可以这样做

var keyValue =  gvShowComm.DataKeys[e.RowIndex].Value;
if(keyValue!=null)
int Product_ID = Convert.ToInt32(keyValue);

,否则

您可以使用 e.NewValues[]e.OldValues[]

Something like Produce_ID sounds like a key field and if it is, you can do

var keyValue =  gvShowComm.DataKeys[e.RowIndex].Value;
if(keyValue!=null)
int Product_ID = Convert.ToInt32(keyValue);

or else

there is e.NewValues[] and e.OldValues[] that you could use.

被你宠の有点坏 2024-11-07 14:37:22

GridViewUpdateEventArgs 具有以下属性:允许您访问 旧值NewValues 特定行的集合。

可以像这样访问它:

protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Product_ID is a column that I am displaying in my GV!
    int Product_ID = Int32.Parse(e.Keys["Product_ID"]);

    Entity_Product_Point ev = new Entity_Product_Point();

    ev.MyDateTime = DateTime.Parse(e.NewValues["MyProp"]);

    // And so on...
}

但这很难! 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:

protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Product_ID is a column that I am displaying in my GV!
    int Product_ID = Int32.Parse(e.Keys["Product_ID"]);

    Entity_Product_Point ev = new Entity_Product_Point();

    ev.MyDateTime = DateTime.Parse(e.NewValues["MyProp"]);

    // And so on...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文