I'm writing a web application using .NET 4.0. I've got a gridview which gets its data from an entity datasource. The underlying data consists of:
- title [string]
- done [bit]
The gridview allows rows to be edited. This works fine for the title. It also works for the 'done' field, but only if it's a string. So I can edit it by typing true or false. I don't want this to be typed, I want to use a checkbox for this. I'm using a templatefield
to create the checkbox. This shows whether or not the 'done' boolean is true. After clicking the 'edit' button it's possible to check/uncheck the 'done' checkbox. So far so good. But when I click the 'update' button the value of the 'done' field is not updated.
So in short; how can I update a bit (boolean) value in my database with a gridview displaying the bit as a checkbox with entitydatasource as the source for the gridview?
You need to do coding in server side in greidview updating event or on rowcommand event, gridview row.findcontrol("chkDone") in each raw and get the state weather it checked or not....and as per that state you can update the database..
MARK AS ANSWER IF IT WORKS....
Mark Answered if it helps - Good luck!
Cheers!
- Kaushik Patel
Marked as answer by AartMan on Oct 25, 2011 11:27 AM
Ok, so here's what I've done. I've added the onrowupdating event to the gridview, and in the event I've looked up the chkDone control. The entity data source was no problem so I haven't changed anything for that. Here's the gridview and the code behind:
Thanks again for yor answer Kaushik. I wish that the gridview would create a checkbox for a boolean automatically, who wants their users to display or even edit TRUE / FALSE anyway ;)
AartMan
Member
5 Points
4 Posts
checkbox not updating in gridview based on entity datasource
Oct 24, 2011 10:19 AM|LINK
Hi all,
I'm writing a web application using .NET 4.0. I've got a gridview which gets its data from an entity datasource. The underlying data consists of:
- title [string]
- done [bit]
The gridview allows rows to be edited. This works fine for the title. It also works for the 'done' field, but only if it's a string. So I can edit it by typing true or false. I don't want this to be typed, I want to use a checkbox for this. I'm using a templatefield to create the checkbox. This shows whether or not the 'done' boolean is true. After clicking the 'edit' button it's possible to check/uncheck the 'done' checkbox. So far so good. But when I click the 'update' button the value of the 'done' field is not updated.
Here is the code I'm using.
<asp:GridView ID="gvSession" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="SessionID" DataSourceID="edsSessions" > <Columns> <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="true" SortExpression="Title" /> <asp:TemplateField ShowHeader="True" HeaderText="Done"> <ItemTemplate> <asp:CheckBox ID="chkDone" runat="server" Checked='<%# Convert.ToBoolean(Eval("Done")) %>' Text='<%# Convert.ToBoolean(Eval("Done")) %>' Enabled="false" /> </ItemTemplate> <EditItemTemplate> <asp:CheckBox ID="chkDone" runat="server" Checked='<%# Convert.ToBoolean(Eval("Done")) %>' Text='<%# Convert.ToBoolean(Eval("Done")) %>' Enabled="true"/> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="false" CommandName="Update" Text="Update" /> <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="false" CommandName="Delete" OnClientClick='return confirm("Do you wish to delete selected data?");' Text="Delete" /> <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:EntityDataSource ID="edsSessions" runat="server" ConnectionString="name=Entities" DefaultContainerName="Entities" EnableFlattening="False" EnableDelete="true" EnableUpdate="true" EntitySetName="Sessions" </asp:EntityDataSource>So in short; how can I update a bit (boolean) value in my database with a gridview displaying the bit as a checkbox with entitydatasource as the source for the gridview?
kaushik_tatv...
Contributor
2808 Points
500 Posts
Re: checkbox not updating in gridview based on entity datasource
Oct 24, 2011 11:17 AM|LINK
You need to do coding in server side in greidview updating event or on rowcommand event, gridview row.findcontrol("chkDone") in each raw and get the state weather it checked or not....and as per that state you can update the database..
MARK AS ANSWER IF IT WORKS....
Cheers!
- Kaushik Patel
AartMan
Member
5 Points
4 Posts
Re: checkbox not updating in gridview based on entity datasource
Oct 24, 2011 03:22 PM|LINK
Ok. Thanks for your answer. That should work. I was hoping to fix this without doing the update manually from code behind.
I will try this tomorrow, when it works I'll post the result.
AartMan
Member
5 Points
4 Posts
Re: checkbox not updating in gridview based on entity datasource
Oct 25, 2011 11:27 AM|LINK
Ok, so here's what I've done. I've added the onrowupdating event to the gridview, and in the event I've looked up the chkDone control. The entity data source was no problem so I haven't changed anything for that. Here's the gridview and the code behind:
<asp:GridView ID="gvSession" runat="server" OnRowUpdating="gvSession_RowUpdating" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="SessionID" DataSourceID="edsSessions" > <Columns> <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="true" SortExpression="Title" /> <asp:TemplateField ShowHeader="True" HeaderText="Done"> <ItemTemplate> <asp:CheckBox ID="chkDone" runat="server" Checked='<%# Convert.ToBoolean(Eval("Done")) %>' Text='<%# Convert.ToBoolean(Eval("Done")) %>' Enabled="false" /> </ItemTemplate> <EditItemTemplate> <asp:CheckBox ID="chkDone" runat="server" Checked='<%# Convert.ToBoolean(Eval("Done")) %>' Text='<%# Convert.ToBoolean(Eval("Done")) %>' Enabled="true"/> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="false" CommandName="Update" Text="Update" /> <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="false" CommandName="Delete" OnClientClick='return confirm("Do you wish to delete selected data?");' Text="Delete" /> <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void gvSession_RowUpdating(object sender, GridViewUpdateEventArgs e) { using (var context = new Entities()) { var sessionID = Convert.ToInt32(e.Keys[0]); var session = (from s in context.Sessions where s.SessionID == sessionID select s).FirstOrDefault(); try { GridViewRow row = gvSession.Rows[e.RowIndex]; CheckBox check = (CheckBox)row.FindControl("chkDone"); if (check.Checked) { session.Done = true; } else { session.Done = false; } context.SaveChanges(); } catch (Exception) { e.Cancel = true; } } }Thanks again for yor answer Kaushik. I wish that the gridview would create a checkbox for a boolean automatically, who wants their users to display or even edit TRUE / FALSE anyway ;)