网格视图命令和所选行

本文关键字:视图 命令 网格 | 更新日期: 2023-09-27 18:26:01

我正在尝试获取选定的行数据,并将其传输到同一页上的标签。但是,我无法使Gridview.SelectedRow与我的CommandName一起使用。我什么都试过了。。。。

我明白错误。对象引用未设置为对象的实例。标签上2.文本

这是我的代码:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if (e.CommandName == "Grab")
    {
        Label2.Text = GridView1.SelectedRow.Cells[2].Text;
        Label3.Text = GridView1.SelectedRow.Cells[3].Text;
        Label4.Text = GridView1.SelectedRow.Cells[4].Text;
        Label5.Text = GridView1.SelectedRow.Cells[5].Text;
    }
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" CssClass="td" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="574px" onrowcommand="GridView1_RowCommand">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/edit.png" OnClick="ImageButton2_Click" />
                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Select" ImageUrl="~/images/delete.png" onclientclick=" return confirm('Are you want to Delete this Vehicle?');" />
                <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/images/refre.png"  CommandName="Grab" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
        <asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" />
        <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
        <asp:BoundField DataField="Submodel" HeaderText="Submodel" SortExpression="Submodel" />
        <asp:BoundField DataField="ISENABLED" HeaderText="ISENABLED" SortExpression="ISENABLED" />
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FBFBF2" />
    <SortedAscendingHeaderStyle BackColor="#848384" />
    <SortedDescendingCellStyle BackColor="#EAEAD3" />
    <SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>

网格视图命令和所选行

试试这样的方法。由于您试图访问command事件中的值,而不是OnSelectedIndexChanged事件中的数值,因此需要首先获取触发命令事件的行。

if (e.CommandName == "Grab")
{
      GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
      if (row != null)
      {
          Label2.Text = row.Cells[2].Text;
          Label3.Text = row.Cells[3].Text;
          Label4.Text = row.Cells[4].Text;
          Label5.Text = row.Cells[5].Text;
      }
}