0

我目前在我的 asp.net 应用程序中有这个 gridview OnRowDataBound 方法:

protected void gvwStatus_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string excsCost = e.Row.Cells[3].Text.ToString();
        //do stuff//
    }
}

标记:

                                  <asp:GridView ID="gvwStatus" runat="server" CssClass="gridview" ShowHeaderWhenEmpty="True"
                                        AllowPaging="True" BackColor="Control" OnSorting="gvw_OnSorting" OnPageIndexChanging="gvwStatus_PageIndexChanging"
                                        OnRowDataBound="gvwStatus_OnRowDataBound" AllowSorting="True" AutoGenerateColumns="False"
                                        Width="98%" PageSize="100" Style="margin-left: 10px;">
                                        <Columns>
                                            <asp:TemplateField HeaderText="Item" SortExpression="ITEM_NO">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lbtnItemNo" runat="server" CssClass="app_hyperlink" Text='<%#Eval("ITEM_NO") %>'
                                                        OnCommand="lbtnItemNo_OnClick" CommandArgument='<%#Eval("ITEM_NO") %>' />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:BoundField DataField="ITEM_OBS_STATUS" HeaderText="Obs Status" SortExpression="ITEM_OBS_STATUS" />
                                            <asp:BoundField DataField="MIP_NO" HeaderText="MIP" SortExpression="MIP_NO" />
                                            <asp:BoundField DataField="LOT_FROM_EFF_DATE" HeaderText="Chgovr Date" SortExpression="LOT_FROM_EFF_DATE" />
                                            <asp:BoundField DataField="TRGT_CHGOVR_DATE" HeaderText="Target Date" SortExpression="TRGT_CHGOVR_DATE" />
                                            <asp:TemplateField HeaderText="Excess Cost" SortExpression="EXCS_ITEM_COST_TOT">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblExcessCostStatus" runat="server" CssClass="app_label" Text='<%#string.Format("{0:C}", Eval("EXCS_ITEM_COST_TOT")) %>' />
                                                </ItemTemplate>
                                                <ItemStyle HorizontalAlign="Right" />
                                            </asp:TemplateField>
                                            <asp:BoundField DataField="SENT_TS" HeaderText="Sent to Plants/Vndrs" SortExpression="SENT_TS" />
                                        </Columns>
                                        <AlternatingRowStyle BackColor="White" />
                                        <PagerStyle HorizontalAlign="Left" />
                                        <PagerSettings Mode="NumericFirstLast" />
                                        <HeaderStyle CssClass="gvLinkNoUnderline" />
                                    </asp:GridView>

索引在此处无法正常运行。对于我的字符串 excsCost,我指定我想要索引 3(或单元格 4)中的值,但我从索引 5(单元格 6)中获取值。当我指定索引 2 时,我从索引 0 中获取值。似乎完全未对齐。

有人熟悉这个问题吗?

4

1 回答 1

0

实际上您正在使用ItemTmplate,因此您可以通过编程方式获取 Control 并在您的代码中对其进行操作

   protected void gvwStatus_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {


      if (e.Row.RowType == DataControlRowType.DataRow)
      {
         Label lblExcsCost = new Label();
         lblExcsCost = e.Row.FindControl("lblExcessCostStatus") as Label;
         string excsCost = lblExcsCost.Text();
      }
    }
于 2012-05-17T11:35:19.937 回答