locked
How to keep 'Absent' text in the place of 'Empty' cell base on Current HeaderName Date in gridview row data bound RRS feed

  • Question

  • User-471420332 posted

    Trying cell value should be 'Absent' if cell is empty base on headertext date less than and equal to today date that cell should empty, below 12,13 dates are matching with below condition but how to cell field absent base on header text date, header condition is matching with below if condition but not getting how to keep absent.

     protected void gridview_trainees_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                GridView gridView = (GridView)sender;
                var colCount = gridview_trainees.Columns.Count;
                for (int i = 3; i < colCount; i++)
                {
                    BoundField columField = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
                    DateTime CurrentColumndate = DateTime.Parse(columField.HeaderText);
    
    
                    if (CurrentColumndate.Date <=DateTime.Now.Date)
                    {
                        //trying cell value should be absent if cell is empty base on header
                    }
    
                }
    
    
            }
        }

    My current gridview is like below

    enter image description hereI want ouput like this, if header text date <= today date that column empty cell should be 'Absent'

    enter image description here

    Friday, December 13, 2019 11:43 AM

Answers

  • User409696431 posted

    You can't test for the null cell values inside

     if (e.Row.RowType == DataControlRowType.Header)
            {

    That only triggers on the header row.

    You'll need to save the values for each date header (either yes it's less than or equal to today, or it's not), and then do another "if" for type DataRow instead of Header.  In that if statement check the cells for null and if the date condition for that column requires it, change the value to Absent.

    Alternatively if you don't want to create a list of the saved results of the condition test, in the DataRow if statement you can fetch the data row's header text using

    string headerRowText = gridView.HeaderRow.Cells[i].Text;

    This involves doing the date comparisons every row instead of just once when you read the header. If the GridView is small the difference might not be noticable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 13, 2019 10:22 PM
  • User-1716253493 posted

    Because you want access header and the rows, use DataBound instead

        protected void gridview_trainees_DataBound(object sender, EventArgs e)
        {
            
            if (gridview_trainees.Rows.Count > 0)
            {
                DateTime CurrentColumndate = DateTime.Parse(gridview_trainees.HeaderRow.Cells[3].Text);
                foreach(GridViewRow row in gridview_trainees.Rows)
                {
                    if(CurrentColumndate.Date<=DateTime.Now.Date && string.IsNullOrWhiteSpace(row.Cells[3].Text))
                    {
                        row.Cells[3].Text = "Absent";
                    }
                }
            }
        }

    Above sample for single column only, modify it to meet your requirement (multicolumn)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 14, 2019 7:44 AM
  • User288213138 posted

    Hi mazhar,

    I want ouput like this, if header text date <= today date that column empty cell should be 'Absent'

    According to your description, i made demo for you.

    I stored the header value in a session, then traverse the GridView and compare the session value with the current date.

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
    
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Traineeld"), new DataColumn("Name"), new DataColumn("Mobile"), new DataColumn("12/12/2019") });
                    dt.Rows.Add(1, "mazhar1", 12345);
                    dt.Rows.Add(2, "mazhar2", 12345);
                    dt.Rows.Add(3, "mazhar3", 123456);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
    
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                          
                if (e.Row.RowType == DataControlRowType.Header)
                {             
                    Session["dt1"] = e.Row.Cells[3].Text;            
                }
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
                {
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
                  
                    if (DateTime.Parse(Session["dt1"].ToString()) <= DateTime.Now.Date)
                    {
                        gvRow.Cells[3].Text = "Absent";
                    }
                }          
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 17, 2019 7:39 AM

All replies

  • User409696431 posted

    You can't test for the null cell values inside

     if (e.Row.RowType == DataControlRowType.Header)
            {

    That only triggers on the header row.

    You'll need to save the values for each date header (either yes it's less than or equal to today, or it's not), and then do another "if" for type DataRow instead of Header.  In that if statement check the cells for null and if the date condition for that column requires it, change the value to Absent.

    Alternatively if you don't want to create a list of the saved results of the condition test, in the DataRow if statement you can fetch the data row's header text using

    string headerRowText = gridView.HeaderRow.Cells[i].Text;

    This involves doing the date comparisons every row instead of just once when you read the header. If the GridView is small the difference might not be noticable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 13, 2019 10:22 PM
  • User-1716253493 posted

    Because you want access header and the rows, use DataBound instead

        protected void gridview_trainees_DataBound(object sender, EventArgs e)
        {
            
            if (gridview_trainees.Rows.Count > 0)
            {
                DateTime CurrentColumndate = DateTime.Parse(gridview_trainees.HeaderRow.Cells[3].Text);
                foreach(GridViewRow row in gridview_trainees.Rows)
                {
                    if(CurrentColumndate.Date<=DateTime.Now.Date && string.IsNullOrWhiteSpace(row.Cells[3].Text))
                    {
                        row.Cells[3].Text = "Absent";
                    }
                }
            }
        }

    Above sample for single column only, modify it to meet your requirement (multicolumn)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 14, 2019 7:44 AM
  • User288213138 posted

    Hi mazhar,

    I want ouput like this, if header text date <= today date that column empty cell should be 'Absent'

    According to your description, i made demo for you.

    I stored the header value in a session, then traverse the GridView and compare the session value with the current date.

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
    
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Traineeld"), new DataColumn("Name"), new DataColumn("Mobile"), new DataColumn("12/12/2019") });
                    dt.Rows.Add(1, "mazhar1", 12345);
                    dt.Rows.Add(2, "mazhar2", 12345);
                    dt.Rows.Add(3, "mazhar3", 123456);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
    
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                          
                if (e.Row.RowType == DataControlRowType.Header)
                {             
                    Session["dt1"] = e.Row.Cells[3].Text;            
                }
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
                {
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
                  
                    if (DateTime.Parse(Session["dt1"].ToString()) <= DateTime.Now.Date)
                    {
                        gvRow.Cells[3].Text = "Absent";
                    }
                }          
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 17, 2019 7:39 AM