Sandeep Kumar

Sandeep Kumar

  • 1k
  • 683
  • 59.5k

How to determine last column index of a gridview through rowdatabound

Feb 17 2022 7:22 AM

i have binded Dynammic gridview with datatable means no header text

<asp:GridView ID="grdResult" runat="server" CssClass="table-grid" OnRowDataBound="grdResult_RowDataBound"> 
            <EmptyDataTemplate>
                <asp:Label ID="Label6" runat="server" Text="Record Not Found."></asp:Label>
            </EmptyDataTemplate>
        </asp:GridView>

i want find last column index in row data bound

protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int lastcolumnIndex = 0;
        int Count = 0;
      
            if (e.Row.RowType == DataControlRowType.Header)
        {

           Count = grdResult.HeaderRow.Cells.Count;
           lastcolumnIndex = Convert.ToInt32(grdResult.HeaderRow.Cells.Count - 1);
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[lastcolumnIndex].Text.ToString().Length > 20)
            {
                e.Row.Cells[lastcolumnIndex].ToolTip = e.Row.Cells[lastcolumnIndex].Text.ToString();
                e.Row.Cells[lastcolumnIndex].Text = e.Row.Cells[lastcolumnIndex].Text.ToString().Substring(0, 20) + "...";
            }
        }
    }


Answers (1)