【问题标题】:how to get data from a gridview如何从网格视图中获取数据
【发布时间】:2015-02-04 03:07:04
【问题描述】:

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

首先是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")));  

}

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

现在是从按下按钮的行获取“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);
   }
}

但是,获取选定的行并从行中读出订单标识对我来说具有挑战性。我一遍又一遍地凝视着这个,看不出我在哪里犯了错误。

行:

int index = Convert.ToInt32(e.CommandArgument)

失败

输入的字符串格式不正确

如果在微软的 msdn 页面上也发现了这个?!

如果我显示 e.commandArgument 我会看到:&lt;% ((GridViewRow) Container).RowIndex %&gt;

谁能帮助我从客户按下重新发送按钮的行中获取TD_OrdID 的值OrdId

提前谢谢你。

【问题讨论】:

    标签: c# asp.net gridview rowcommand


    【解决方案1】:
    <% ((GridViewRow) Container).RowIndex %>
    

    你错过了#

    试试:

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

    【讨论】:

    • 修复了错误但我现在得到的值为零?
    • 索引 = 0 ?或 ordId = 0 ?
    • 对不起,一切都好 - 没看到我的错字。非常感谢 :) 现在可以使用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2012-06-20
    • 1970-01-01
    • 2023-03-19
    • 2012-08-08
    • 2015-03-25
    相关资源
    最近更新 更多