简体   繁体   English

在GridView中访问项目

[英]Accessing items in GridView

I have a GridView and I am updating it through LINQ through C#. 我有一个GridView,并且正在通过C#通过LINQ更新它。 I want to access the elements in my GridView and store them in the variable. 我想访问GridView中的元素并将它们存储在变量中。 Then I will pass these variables into my LINQ query. 然后,我将这些变量传递到我的LINQ查询中。

How should I access the items in my GridView? 我应该如何访问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>

Something like Produce_ID sounds like a key field and if it is, you can do 类似于Produce_ID的声音听起来像一个关键字段,如果是,则可以

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. 您可以使用e.NewValues[]e.OldValues[]

The GridViewUpdateEventArgs has properties on it that allow you to get at the Keys , OldValues , and NewValues collections for a particular row. GridViewUpdateEventArgs上具有属性,使您可以获取特定行的KeysOldValuesNewValues集合。

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. ASP.Net可以使用可用的DataSource类之一为您完成工作。 For your purposes you should look into the LinqDataSource 为了您的目的,您应该查看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. 值得注意的是,尽管DataSource类既有用又好用,但它们具有很多不可思议的作用,您几乎可以排除使用任何可测试模式的可能性。 (see WebFormsMVP ) (请参阅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 . 在这种情况下,您将需要围绕业务逻辑实现外观包装,并使用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. 无论哪种方式,您都比手动扎根于键/值对集合中寻找值,然后将其转换回其正确类型更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM