从GridView获取列值

时间:2015-05-23 12:11:23

标签: c# asp.net aspxgridview datagridviewcolumn

我是ASP.NET的新手,我使用GridView在网页上显示数据。我还在Gridview中添加了一个名为流程订单的按钮。

<asp:GridView ID="GridView1" runat="server" autogenerateselectbutton="True"     GridLines="None" AllowPaging ="true" 
        OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
        >

        <Columns>

    <asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="Process" runat="server" 
      CommandName="processorders" 
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Process orders" />
  </ItemTemplate> 
</asp:TemplateField>


        </Columns>
        </asp:GridView>

我使用row命令方法获取行的选定索引。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "processorders")
        {
    // Retrieve the row index stored in the 
    // CommandArgument property.
            int index = Convert.ToInt32(e.CommandArgument);
    }

我也为默认选择按钮实现了selectedIndexChanged方法:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        txtbox.Text = "You selected " + row.Cells[3].Text + ".";
    }

如果我尝试在GridView1_RowCommand中使用GridViewRow,我会得到一个空指针异常。该行始终为null,但在GridView1_SelectedIndexChanged中可以正常工作。 我想要实现的是,使用我的Process按钮从所选行获取列值,然后使用该值更新数据库。我不想使用Select来做。 有没有什么办法可以使用row.Cells []而无需指定索引。我想获取列名的值。 救命?请??

0 个答案:

没有答案