in RowdataBound event of the GridView check the time displayed in the relevant column, compare it with the current period and based on this set the edit button Enabled / Disabled
Kindly mark this post as "Answer", if it helped you.
in RowdataBound event of the GridView check the time displayed in the relevant column, compare it with the current period and based on this set the edit button Enabled / Disabled
Here is a sample code. It allows to edit a row only if inserted time and the editing time are between 08:00 hrs and 14:00 hrs on a date. You may customize this and use asper your actual requirements.
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btnEdit As Button = DirectCast(e.Row.FindControl("EditButton"), Button)
Dim insertedTime As DateTime = Convert.ToDateTime(e.Row.Cells(0).Text.Trim())
Dim editTime As DateTime = DateTime.Now
Dim thisDay As DateTime = DateTime.Today
Dim AllowedEditS1 As String = thisDay.ToString("dd-MM-yyyy") & " 08:00"
Dim AllowedEditE1 As String = thisDay.ToString("dd-MM-yyyy") & " 14:00"
Dim allowedEditStart1 As DateTime = Convert.ToDateTime(AllowedEditS1)
Dim allowedEditEnd1 As DateTime = Convert.ToDateTime(AllowedEditE1)
Dim isTimeBetween As Boolean = insertedTime >= allowedEditStart1 AndAlso insertedTime <= allowedEditEnd1
Dim editAllowed As Boolean = editTime >= allowedEditStart1 AndAlso editTime <= allowedEditEnd1
If isTimeBetween = True AndAlso editAllowed = False Then
btnEdit.Enabled = False
End If
End If
End Sub
In C# code used one"}" is missing at the end That's why you get error while converting.
Kindly mark this post as "Answer", if it helped you.
salim20001
Member
154 Points
507 Posts
Locking editing in a gridview
May 11, 2012 12:14 AM|LINK
Hi,
I had a grid view with the edit button, however I would like that
*Data inseted from 8h00 to 14h00 be editable only in this time intervall
*at 14h to 8h00, old data are no more editable, but data inserted at this time (14h-8h) is still editable untill 8h00
*etc...
Can Anyone help me with this ?
mohammed.kam...
Member
139 Points
92 Posts
Re: Locking editing in a gridview
May 11, 2012 12:28 AM|LINK
in the row edit event fired check the time from tat row and if it fits your condition then do the editing otherwise call the cancel command
OWDARO
basheerkal
Star
10672 Points
2426 Posts
Re: Locking editing in a gridview
May 11, 2012 01:03 AM|LINK
in RowdataBound event of the GridView check the time displayed in the relevant column, compare it with the current period and based on this set the edit button Enabled / Disabled
(Talk less..Work more)
salim20001
Member
154 Points
507 Posts
Re: Locking editing in a gridview
May 11, 2012 05:36 PM|LINK
Can you give me an example ?
Thanks
basheerkal
Star
10672 Points
2426 Posts
Re: Locking editing in a gridview
May 11, 2012 09:07 PM|LINK
Here is a sample code. It allows to edit a row only if inserted time and the editing time are between 08:00 hrs and 14:00 hrs on a date. You may customize this and use asper your actual requirements.
in form
<form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" PageSize="2" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="insertedAT" HeaderText="insertedAT" SortExpression="insertedAT" /> <asp:BoundField DataField="insertedby" HeaderText="insertedby" SortExpression="insertedby" /> <asp:TemplateField HeaderText="Edit"> <ItemTemplate> <asp:Button ID="EditButton" runat="server" Height="26px" Text="Edit" CommandName ="Edit"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT [insertedAT], [insertedby] FROM [timecheck]"></asp:SqlDataSource> <br /> </form>C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Button btnEdit = (Button)(e.Row.FindControl("EditButton")); DateTime insertedTime = Convert.ToDateTime(e.Row.Cells[0].Text.Trim()); DateTime editTime = DateTime.Now; DateTime thisDay = DateTime.Today; string AllowedEditS1 = thisDay.ToString("dd-MM-yyyy") + " 08:00"; string AllowedEditE1 = thisDay.ToString("dd-MM-yyyy") + " 14:00"; DateTime allowedEditStart1 = Convert.ToDateTime(AllowedEditS1); DateTime allowedEditEnd1 = Convert.ToDateTime(AllowedEditE1); bool isTimeBetween = insertedTime >= allowedEditStart1 && insertedTime <= allowedEditEnd1; bool editAllowed = editTime >= allowedEditStart1 && editTime <= allowedEditEnd1; if (isTimeBetween == true && editAllowed == false) { btnEdit.Enabled = false; } }(Talk less..Work more)
salim20001
Member
154 Points
507 Posts
Re: Locking editing in a gridview
May 14, 2012 09:10 PM|LINK
Can u help me convert to VB.net , I tried the link: http://www.developerfusion.com/tools/convert/csharp-to-vb/ but didn't work
basheerkal
Star
10672 Points
2426 Posts
Re: Locking editing in a gridview
May 15, 2012 02:19 AM|LINK
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim btnEdit As Button = DirectCast(e.Row.FindControl("EditButton"), Button) Dim insertedTime As DateTime = Convert.ToDateTime(e.Row.Cells(0).Text.Trim()) Dim editTime As DateTime = DateTime.Now Dim thisDay As DateTime = DateTime.Today Dim AllowedEditS1 As String = thisDay.ToString("dd-MM-yyyy") & " 08:00" Dim AllowedEditE1 As String = thisDay.ToString("dd-MM-yyyy") & " 14:00" Dim allowedEditStart1 As DateTime = Convert.ToDateTime(AllowedEditS1) Dim allowedEditEnd1 As DateTime = Convert.ToDateTime(AllowedEditE1) Dim isTimeBetween As Boolean = insertedTime >= allowedEditStart1 AndAlso insertedTime <= allowedEditEnd1 Dim editAllowed As Boolean = editTime >= allowedEditStart1 AndAlso editTime <= allowedEditEnd1 If isTimeBetween = True AndAlso editAllowed = False Then btnEdit.Enabled = False End If End If End SubIn C# code used one"}" is missing at the end That's why you get error while converting.
(Talk less..Work more)
salim20001
Member
154 Points
507 Posts
Re: Locking editing in a gridview
May 31, 2012 10:03 PM|LINK
Can you help do the same thing for the period 14h-08h ?