locked
Creating header row for child gridview error RRS feed

  • Question

  • User1406973109 posted

    Good day all,

    Please i'm having difficulty creating header row for a nested gridview, what I noticed is that the client id is showing something else from the id of the gridview. This is my code for the onrowdatabound event for the parent gridview below:

    protected void grdAcademicExpress_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strAdmissionNumber = grdAcademicExpress.DataKeys[e.Row.RowIndex].Value.ToString();
                    //string strClass = grdAcademicExpress.DataKeys[e.Row.RowIndex].Values[1].ToString();
                    GridView grdViewStudent = e.Row.FindControl("grdViewStudent") as GridView;
    
                    //Addition start here
                    GridViewRow row = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
                    TableHeaderCell cell = new TableHeaderCell();
                    cell.Text = "1ST TERM";
                    cell.ColumnSpan = 4;
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 3;
                    cell.Text = "2ND TERM";
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 6;
                    cell.Text = "3RD TERM";
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 5;
                    cell.Text = "ANNUAL";
                    row.Controls.Add(cell);
    
    //error occurs here, object reference not set to an instance(client id shows grdAcademicExpress_grdViewStudent)                
    grdViewStudent.HeaderRow.Parent.Controls.AddAt(0, row);
                    
    
                    grdViewStudent.DataSource = objDt.Where(x => x.AdmissionNumber == strAdmissionNumber).ToList();
                    grdViewStudent.DataBind();
                }
            }

    Wednesday, November 6, 2019 6:04 AM

Answers

  • User-719153870 posted

    Hi timotech,

    timotech

    what I noticed is that the client id is showing something else from the id of the gridview.

    According to your code, it seems you are trying to find the HeaderRow of this gridview before it has rows.

    Please modify your code like below and then have a try:

    protected void grdAcademicExpress_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strAdmissionNumber = grdAcademicExpress.DataKeys[e.Row.RowIndex].Value.ToString();
                    //string strClass = grdAcademicExpress.DataKeys[e.Row.RowIndex].Values[1].ToString();
                    GridView grdViewStudent = e.Row.FindControl("grdViewStudent") as GridView;
                    grdViewStudent.DataSource = objDt.Where(x => x.AdmissionNumber == strAdmissionNumber).ToList();
                    grdViewStudent.DataBind();
                    //Addition start here
                    GridViewRow row = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
                    TableHeaderCell cell = new TableHeaderCell();
                    cell.Text = "1ST TERM";
                    cell.ColumnSpan = 4;
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 3;
                    cell.Text = "2ND TERM";
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 6;
                    cell.Text = "3RD TERM";
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 5;
                    cell.Text = "ANNUAL";
                    row.Controls.Add(cell);
    
    //error occurs here, object reference not set to an instance(client id shows grdAcademicExpress_grdViewStudent)                
    grdViewStudent.HeaderRow.Parent.Controls.AddAt(0, row);
                    
                }
            }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 7:24 AM

All replies

  • User-719153870 posted

    Hi timotech,

    timotech

    what I noticed is that the client id is showing something else from the id of the gridview.

    According to your code, it seems you are trying to find the HeaderRow of this gridview before it has rows.

    Please modify your code like below and then have a try:

    protected void grdAcademicExpress_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strAdmissionNumber = grdAcademicExpress.DataKeys[e.Row.RowIndex].Value.ToString();
                    //string strClass = grdAcademicExpress.DataKeys[e.Row.RowIndex].Values[1].ToString();
                    GridView grdViewStudent = e.Row.FindControl("grdViewStudent") as GridView;
                    grdViewStudent.DataSource = objDt.Where(x => x.AdmissionNumber == strAdmissionNumber).ToList();
                    grdViewStudent.DataBind();
                    //Addition start here
                    GridViewRow row = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
                    TableHeaderCell cell = new TableHeaderCell();
                    cell.Text = "1ST TERM";
                    cell.ColumnSpan = 4;
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 3;
                    cell.Text = "2ND TERM";
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 6;
                    cell.Text = "3RD TERM";
                    row.Controls.Add(cell);
    
                    cell = new TableHeaderCell();
                    cell.ColumnSpan = 5;
                    cell.Text = "ANNUAL";
                    row.Controls.Add(cell);
    
    //error occurs here, object reference not set to an instance(client id shows grdAcademicExpress_grdViewStudent)                
    grdViewStudent.HeaderRow.Parent.Controls.AddAt(0, row);
                    
                }
            }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 7:24 AM
  • User1406973109 posted

    GridView grdViewStudent = e.Row.FindControl("grdViewStudent") as GridView;
                    grdViewStudent.DataSource = objDt.Where(x => x.AdmissionNumber == strAdmissionNumber).ToList();
                    grdViewStudent.DataBind();

    Thanks a lot Yang Shen, this has resolved the issue. I appreciate

    Tim

    Wednesday, November 6, 2019 8:53 AM