locked
Gridview RRS feed

  • Question

  • User-1499457942 posted

    Hi

      In Gridview row i have field Date . I want if Date is Null or 01/01/1753 then it should display as Blank.

    Thanks

    Saturday, November 10, 2018 12:33 PM

Answers

  • User1992938117 posted

    There are various ways to handle it.

    1. In Stored Procedure.
    2. In c# code
    3. While bindng to Grid using RowDataBound event

    See sample code using RowDataBound:

    Designer:

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
        </asp:GridView>

    Code:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable table = GetTable();
    
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
    
            private static DataTable GetTable()
            {
                using (DataTable table = new DataTable())
                {
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Date", typeof(DateTime));
    
                    table.Rows.Add("David", DateTime.Now.AddMonths(-5));
                    table.Rows.Add("Sam", DateTime.Now.AddMonths(-2));
                    table.Rows.Add("ChristOff",Convert.ToDateTime("01/01/1753"));
                    table.Rows.Add("Janet", DateTime.Now);
                    table.Rows.Add("Melanie", Convert.ToDateTime("01/01/1753"));
                    return table;
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                //Checking the RowType of the Row  
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[1].Text == "1/1/1753 12:00:00 AM")
                    {
                        e.Row.Cells[1].Text = string.Empty;
                    }
                }
            }

    ##Always try to provide the code you tried and what issue you are getting.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 10, 2018 2:08 PM

All replies

  • User475983607 posted

      In Gridview row i have field Date . I want if Date is Null or 01/01/1753 then it should display as Blank.

    This is another question that you've asked a bunch of time and where we have provided sample code.  Please review your previous threads.  Post the code that you have tried and explain the expected results and actual results.

    Generally, this situation is due to missing validation when saving a record.  You should look into fixing the INSERT/UPDATE logic.  Then this problem goes away as Binding to NULL produces an blank.

    As requested in all your posts, please show the source code.  That way we are not forced to guess how the code works.

    Saturday, November 10, 2018 1:55 PM
  • User1992938117 posted

    There are various ways to handle it.

    1. In Stored Procedure.
    2. In c# code
    3. While bindng to Grid using RowDataBound event

    See sample code using RowDataBound:

    Designer:

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
        </asp:GridView>

    Code:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable table = GetTable();
    
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
    
            private static DataTable GetTable()
            {
                using (DataTable table = new DataTable())
                {
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Date", typeof(DateTime));
    
                    table.Rows.Add("David", DateTime.Now.AddMonths(-5));
                    table.Rows.Add("Sam", DateTime.Now.AddMonths(-2));
                    table.Rows.Add("ChristOff",Convert.ToDateTime("01/01/1753"));
                    table.Rows.Add("Janet", DateTime.Now);
                    table.Rows.Add("Melanie", Convert.ToDateTime("01/01/1753"));
                    return table;
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                //Checking the RowType of the Row  
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[1].Text == "1/1/1753 12:00:00 AM")
                    {
                        e.Row.Cells[1].Text = string.Empty;
                    }
                }
            }

    ##Always try to provide the code you tried and what issue you are getting.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 10, 2018 2:08 PM