本文介绍了获取当前的GridView列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些列的GridView。所有的列都是绑定的:

 < asp:BoundField DataField =orderIdHeaderText =orderId
的SortExpression = 订单ID >< / ASP:绑定列>

最后一栏是:

 < asp:LinkBut​​ton ID =LinkBut​​ton1runat =server
CommandName =onclick =LinkBut​​ton1_ClickText =Button>< / asp:LinkBut​​ton>

正如你所看到的,有一些方法是onclick..就像:

  lbltest.Text = gv_order.Rows [gv_order.SelectedIndex] .Cells [2] .Text; 

通过这段代码,我得到了(offcourse)我在单元格编号为2的选定行上所拥有的内容。我可以从同一行(并从单元格编号2)中获取值,而单击该按钮时没有自选行?例如:当我点击第2行上的按钮 - 我得到该行的单元格2。



这可能吗?

, CommandArgument 属性和 OnRowCommand 这样的事件:

 < asp:GridView(...)OnRowCommand =Gv_RowCommand(...)> 



< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverCommandName =Select
CommandArgument ='<%#绑定(orderId)%>'文本=按钮>< / asp:LinkBut​​ton>

以及后面的代码:

<$ p $
{
if(e.CommandName ==Select)
{
int selectedOrderId = Convert.ToInt32(e.CommandArgument);
// ...
}
}

我希望这是你想要做的。



编辑 - 我对您评论的回答:



然后,它稍微复杂一些,并以某种方式使用'selectedRow'。在我自己的代码中,我使用了这种方法:

 < asp:GridView ID =gv1(...)DataKeyNames =orderId,email,username
OnRowCommand =Gv_RowCommand(...)>



< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverCommandName =Select
CommandArgument ='<%# DataBinder.Eval(Container,RowIndex)%>'Text =Button>
< / asp:LinkBut​​ton>

以及后面的代码:

  protected void Gv_RowCommand(object sender,GridViewCommandEventArgs e)
{
int selectedRowIndex = Convert.ToInt32(e.CommandArgument);
if(e.CommandName ==Select)
{
int orderId = Convert.ToInt32(gv1.DataKeys [selectedRowIndex] [orderId]);
string email = gv1.DataKeys [selectedRowIndex] [email]。ToString();
// ...
}
}


I have GridView with some of columns. all the columns are boundfield like:

            <asp:BoundField DataField="orderId" HeaderText="orderId" 
            SortExpression="orderId"></asp:BoundField>

The last column is:

                    <asp:LinkButton ID="LinkButton1" runat="server"    
  CommandName="" onclick="LinkButton1_Click" Text="Button"></asp:LinkButton>

as you see , there is "onclick" with some method.. like:

        lbltest.Text = gv_order.Rows[gv_order.SelectedIndex].Cells[2].Text;

With that code i get (offcourse) what i have on the selected row in cell number 2. how can i get value from the same row (and from cell number 2) where the button is clicked without the "selceted row" ? example: when i click button on row 2 - i get the cell 2 of that row.

That's possible?

解决方案

If you want to retrieve 'orderid' in more clean way, you can use CommandName, CommandArgument properties and OnRowCommand event like this:

        <asp:GridView (...) OnRowCommand="Gv_RowCommand" (...)>

...

        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"
     CommandArgument='<%# Bind("orderId") %>' Text="Button"></asp:LinkButton>

and in code behind:

protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
         int selectedOrderId = Convert.ToInt32(e.CommandArgument);
         // ... 
    }
}

I hope this is what you want to do.

Edit - my answer to your comment:

Then, it's little more complicated and uses 'selectedRow' in some way. In my own code, I use this approach:

    <asp:GridView ID="gv1" (...) DataKeyNames="orderId,email,username"
         OnRowCommand="Gv_RowCommand" (...)>

            ...

    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"
       CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' Text="Button">
</asp:LinkButton>

and in code behind:

protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int selectedRowIndex = Convert.ToInt32(e.CommandArgument);
    if (e.CommandName == "Select")
    {
         int orderId = Convert.ToInt32(gv1.DataKeys[selectedRowIndex]["orderId"]);
         string email = gv1.DataKeys[selectedRowIndex]["email"].ToString();
         // ... 
    }
}

这篇关于获取当前的GridView列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 19:08