Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I've created form containing Datagridview.
The Datagridview contains "Start Time" and "End Time" columns for employees for every month.
I want to prevent user from insert any row to datagridview at past months.
How can I achive that?
Posted
Updated 29-Sep-14 3:39am
v3
Comments
Sinisa Hajnal 29-Sep-14 9:21am    
Just to clarify: you have the grid that users can normally edit as part of their work? And you want to enable editing only for the current month?
KaushalJB 29-Sep-14 9:27am    
Post your code side gridview where you bind it...or you can refer this link http://forums.asp.net/t/1527401.aspx?disable+gridview+column

You can check in e.g. SelectionChanged whether the row belongs to the past months and if yes just set DataGridView.AllowUserToAddRows Property[^] to false.

Don't forget to set it to true in case you want to allow the adding of the row :)
 
Share this answer
 
v3
I believe what you want to achieve is to make only certain columns read only. This could be done as so..
C#
dataGridView1.Columns["pastmonth"].ReadOnly = true;
 
Share this answer
 
 You need to write this code in onRowDataBoundEvent

protected void grd_OnRowDataBound(object sender, GridviewEventArgs e)
{
DateTime lblEndDate = IRDateUtils.ConvertToDate(End Time); 
                DateTime currentDate = IRDateUtils.ConvertToLocal(Convert.ToDateTime(DateTime.Now.ToShortDateString()));

       if (lblEndDate < currentDate) // here we are comparing end time with current time
         {
              // here show an alert you cant edit this record because time is expired
         }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900