如何获得网格视图中最后一列单元格的值

本文关键字:一列 单元格 网格 何获得 视图 最后 | 更新日期: 2023-09-27 18:06:07

我有一个网格视图,并动态添加行到该网格视图,我想获得单元格5的值为最后一行

  <asp:GridView ID="gridinvoice" runat="server" CssClass="table table-bordered" AutoGenerateColumns="false" OnRowDataBound="gridinvoice_RowDataBound">
      <Columns>
          <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type"></asp:BoundField>
          <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number"></asp:BoundField>
          <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date"  DataFormatString="{0:M/d/yyyy}"></asp:BoundField>
          <asp:BoundField DataField="Debit" HeaderText="Debit" SortExpression="Debit"></asp:BoundField>
          <asp:BoundField DataField="Credit" HeaderText="Credit" SortExpression="Credit"></asp:BoundField>
          <asp:BoundField DataField="Balance" HeaderText="Balance" SortExpression="Balance"></asp:BoundField>
      </Columns>
      <EmptyDataTemplate>
          No records found!
      </EmptyDataTemplate>
      <HeaderStyle Font-Names="Cambria" Font-Italic="False" />
      <RowStyle Font-Names="font-family: 'Bree Serif',serif;" />
 </asp:GridView>

如何获得网格视图中最后一列单元格的值

查看是否有效:

int LastRow = gridinvoice.Rows.Count-1;
string Balance = gridinvoice.Rows[LastRow].Cells[5].Text;

如果你想在后面的代码中这样做,

int lastRowIndex = gridinvoice.Rows.Count - 1;
string value = gridinvoice.Rows[lastRowIndex].Cells[5].Text;

你想访问last RowBalance Column,对吗?

gridinvoice.Rows[gridinvoice.Rows.Count -1].Cells[4].Text