如何从gridview获取数据

时间:2015-02-04 03:07:04

标签: c# asp.net gridview rowcommand

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

首先是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>

现在是数据绑定,以便在客户支付订单时选择重新发送按钮:

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")));  

}

工作正常。我正在禁用该按钮。也许更好的解决方案是隐藏它。然而,这不是我的担忧。

现在使用row命令获取&#34; ordId&#34;按下按钮的行中的值:

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);
   }
}

然而,获取所选行并从行中读出订单标识对我来说是一项挑战。我一遍又一遍地瞪着它,看不出我犯错的地方。

该行:

int index = Convert.ToInt32(e.CommandArgument)

失败
  

输入字符串的格式不正确

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

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

任何人都可以提供帮助,以便我可以从客户按下重新发送按钮的行中获取OrdId的值TD_OrdID吗?

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

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

您错过了#

尝试:

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