如何从网格视图中获取数据

how to get data from a gridview

我在网格视图中显示客户过去的订单。他们可以按最后一列中的重新发送按钮,在订单已支付或超过该状态的情况下重新发送他们的订单。

首先是网格视图:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" pagesize="10" 
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" EmptyDataText="You have no orders yet" 
    GridLines="Vertical" OnRowCommand="GridView1_RowCommand">
    <AlternatingRowStyle BackColor="#DCDCDC"/>
    <Columns>
        <asp:BoundField DataField="TD_OrdID" HeaderText="Ord" InsertVisible="False" ReadOnly="True" SortExpression="TD_OrdID" />
        <asp:BoundField DataField="TD_OrdDate" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Right" HeaderText="Date" SortExpression="TD_OrdDate" ><ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
        <asp:BoundField DataField="TD_PD_PackageName" HeaderText="Package Name" ItemStyle-HorizontalAlign="Left" SortExpression="TD_PD_PackageName" > </asp:BoundField>
        <asp:BoundField DataField="TD_OSName" ItemStyle-HorizontalAlign="Right" HeaderText="Status" SortExpression="TD_OSName" > <ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
        <asp:BoundField DataField="TD_InvID" HeaderText="Inv" InsertVisible="False" ReadOnly="True" SortExpression="TD_InvID" />
        <asp:BoundField DataField="TD_InvPaid" HeaderText="Paid" ItemStyle-HorizontalAlign="Right" SortExpression="TD_InvPaid" ><ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
        <asp:TemplateField>
            <ItemTemplate>
                 <asp:Button runat="server" ID="btnResend" CommandName="Resend" Text="Resend" CommandArgument="<% ((GridViewRow) Container).RowIndex
                      %>" OnDataBinding="btnResend_DataBinding" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" Font-Size="Small" HorizontalAlign="Center"/>
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
    </asp:GridView>

现在数据绑定到 select 客户支付订单时的重新发送按钮:

protected void btnResend_DataBinding(object sender, EventArgs e)
{
    // only set button to enable when customer can resend document!

    Button btn = (Button)(sender);
    // enable button for status                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  emailed, paid and closed 
    btn.Enabled = ((Eval("TD_OSName").ToString().Equals("closed")) ||
                   (Eval("TD_OSName").ToString().Equals("paid")) ||
                   (Eval("TD_OSName").ToString().Equals("emailed")));  

}

效果很好。我正在禁用按钮。也许更好的解决方案是隐藏它。不过这不是我关心的。

现在是从按下按钮的行获取 "ordId" 值的行命令:

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

        // Retrieve the row that contains the button
        // from Rows collection.
        GridViewRow row = GridView1.Rows[index];

        // add code
        int ordId = Convert.ToInt32(row.Cells[0].Text);

        lblMessage.ForeColor = System.Drawing.Color.Green;
        //lblMessage.Text = displayMessages.YourDocumentResent() + "OrdID:" + ordId;
        //lblMessage.Text = displayMessages.YourDocumentResent();
        lblMessage.Text = Convert.ToString(e.CommandArgument);
   }
}

然而,获取 selected 行并从该行中读出订单标识对我来说是一个挑战。我一遍又一遍地盯着这个,看不出我在哪里犯了错误。

行:

int index = Convert.ToInt32(e.CommandArgument)

失败

Input string was not in a correct format

如果在 Microsoft 的 msdn 页面上也能找到这个?!

如果我显示 e.commandArgument 我会看到:<% ((GridViewRow) Container).RowIndex %>

任何人都可以帮助我从客户按下 Resend 按钮的行中的 TD_OrdID 中获取值 OrdId 吗?

提前谢谢你。

<% ((GridViewRow) Container).RowIndex %>

您错过了 #

尝试:

<%# ((GridViewRow) Container).RowIndex %>