locked
Disable updates to gridview based on Date RRS feed

  • Question

  • User1479987514 posted

    Hi All

    I am writing a web application in visual studio with data being read from SQL Server 2012 using Entities. I need a code either in the Front end (GridView) or database that would check the PeriodStart (Date Field) against the current date (System) if the periodstart is greater then the current date, the user must be able to edit it, if not the user must not be able to do anything a message should appear.

    Thanks

    Wednesday, June 22, 2016 8:15 AM

Answers

  • User-1034726716 posted

    You could handle the RowDataBound event of GridView to hide a certain row based on your condition. Here's a quick example:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
                if (e.Row.RowType == DataControlRowType.DataRow) {
                    // assuming the the ShowEditLink resides in the first column of Grid
                    //just changed the index of cells to where the EditLink resides
                    LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0];
    
                    //if you are displaying the PeriodStart in the Grid using BoundField
                    //Cells[1] means you are displaying the date in second column, just change the index
                    //where you display the date
                    DateTime periodStart = Convert.ToDateTime(e.Row.Cells[1].Text);
                    if (lb != null) {
                        if (periodStart > DateTime.Now)
                            lb.Visible = true;
                        else
                            lb.Visible = false;
                    }
                }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 22, 2016 5:54 PM

All replies

  • User-1034726716 posted

    You could handle the RowDataBound event of GridView to hide a certain row based on your condition. Here's a quick example:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
                if (e.Row.RowType == DataControlRowType.DataRow) {
                    // assuming the the ShowEditLink resides in the first column of Grid
                    //just changed the index of cells to where the EditLink resides
                    LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0];
    
                    //if you are displaying the PeriodStart in the Grid using BoundField
                    //Cells[1] means you are displaying the date in second column, just change the index
                    //where you display the date
                    DateTime periodStart = Convert.ToDateTime(e.Row.Cells[1].Text);
                    if (lb != null) {
                        if (periodStart > DateTime.Now)
                            lb.Visible = true;
                        else
                            lb.Visible = false;
                    }
                }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 22, 2016 5:54 PM
  • User36583972 posted

    Hi Poovy_SA,

    As vinz said, you can control your edit function in RowDataBound event. The following links introduce how to get data from database and edit records in GridView.

    Insert Update Edit Delete record in GridView using SqlDataSource in ASP.Net:

    http://www.aspsnippets.com/Articles/Insert-Update-Edit-Delete-record-in-GridView-using-SqlDataSource-in-ASPNet.aspx

    Edit and Update Record in GridView in ASP.Net:

    http://www.c-sharpcorner.com/UploadFile/1e050f/edit-and-update-record-in-gridview-in-Asp-Net/

    Best Regards,

    Yohann Lu

    Thursday, June 23, 2016 10:01 AM