checkbox not updating in gridview based on entity datasourcehttp://forums.asp.net/t/1733278.aspx/1?checkbox+not+updating+in+gridview+based+on+entity+datasourceTue, 25 Oct 2011 11:27:31 -040017332784651797http://forums.asp.net/p/1733278/4651797.aspx/1?checkbox+not+updating+in+gridview+based+on+entity+datasourcecheckbox not updating in gridview based on entity datasource <p>Hi all,</p> <p>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:</p> <p>- title [string]</p> <p>- done [bit]</p> <p>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.</p> <p>Here is the code I'm using.</p> <pre class="prettyprint">&lt;asp:GridView ID=&quot;gvSession&quot; runat=&quot;server&quot; AllowPaging=&quot;True&quot; AllowSorting=&quot;True&quot; AutoGenerateColumns=&quot;False&quot; DataKeyNames=&quot;SessionID&quot; DataSourceID=&quot;edsSessions&quot; &gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&quot;Title&quot; HeaderText=&quot;Title&quot; ReadOnly=&quot;true&quot; SortExpression=&quot;Title&quot; /&gt; &lt;asp:TemplateField ShowHeader=&quot;True&quot; HeaderText=&quot;Done&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&quot;chkDone&quot; runat=&quot;server&quot; Checked='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Text='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Enabled=&quot;false&quot; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:CheckBox ID=&quot;chkDone&quot; runat=&quot;server&quot; Checked='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Text='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Enabled=&quot;true&quot;/&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField ShowHeader=&quot;False&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&quot;lnkEdit&quot; runat=&quot;server&quot; CausesValidation=&quot;False&quot; CommandName=&quot;Edit&quot; Text=&quot;Edit&quot; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:LinkButton ID=&quot;lnkUpdate&quot; runat=&quot;server&quot; CausesValidation=&quot;false&quot; CommandName=&quot;Update&quot; Text=&quot;Update&quot; /&gt; &lt;asp:LinkButton ID=&quot;lnkDelete&quot; runat=&quot;server&quot; CausesValidation=&quot;false&quot; CommandName=&quot;Delete&quot; OnClientClick='return confirm(&quot;Do you wish to delete selected data?&quot;);' Text=&quot;Delete&quot; /&gt; &lt;asp:LinkButton ID=&quot;lnkCancel&quot; runat=&quot;server&quot; CausesValidation=&quot;false&quot; CommandName=&quot;Cancel&quot; Text=&quot;Cancel&quot; /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:EntityDataSource ID=&quot;edsSessions&quot; runat=&quot;server&quot; ConnectionString=&quot;name=Entities&quot; DefaultContainerName=&quot;Entities&quot; EnableFlattening=&quot;False&quot; EnableDelete=&quot;true&quot; EnableUpdate=&quot;true&quot; EntitySetName=&quot;Sessions&quot; &lt;/asp:EntityDataSource&gt;</pre> <p>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?</p> 2011-10-24T10:19:12-04:004651886http://forums.asp.net/p/1733278/4651886.aspx/1?Re+checkbox+not+updating+in+gridview+based+on+entity+datasourceRe: checkbox not updating in gridview based on entity datasource <p>You need to do coding in server side in greidview updating event or on rowcommand event, gridview row.findcontrol(&quot;chkDone&quot;) in each raw and get the state weather it checked or not....and as per that state you can update the database..</p> <p>&nbsp;</p> <p>MARK AS ANSWER IF IT WORKS....</p> 2011-10-24T11:17:25-04:004652244http://forums.asp.net/p/1733278/4652244.aspx/1?Re+checkbox+not+updating+in+gridview+based+on+entity+datasourceRe: checkbox not updating in gridview based on entity datasource <p>Ok. Thanks for your answer. That should work. I was hoping to fix this without doing the update manually from code behind.</p> <p>I will try this tomorrow, when it works I'll post the result.</p> 2011-10-24T15:22:39-04:004653369http://forums.asp.net/p/1733278/4653369.aspx/1?Re+checkbox+not+updating+in+gridview+based+on+entity+datasourceRe: checkbox not updating in gridview based on entity datasource <p>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:</p> <pre class="prettyprint">&lt;asp:GridView ID=&quot;gvSession&quot; runat=&quot;server&quot; OnRowUpdating=&quot;gvSession_RowUpdating&quot; AllowPaging=&quot;True&quot; AllowSorting=&quot;True&quot; AutoGenerateColumns=&quot;False&quot; DataKeyNames=&quot;SessionID&quot; DataSourceID=&quot;edsSessions&quot; &gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&quot;Title&quot; HeaderText=&quot;Title&quot; ReadOnly=&quot;true&quot; SortExpression=&quot;Title&quot; /&gt; &lt;asp:TemplateField ShowHeader=&quot;True&quot; HeaderText=&quot;Done&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&quot;chkDone&quot; runat=&quot;server&quot; Checked='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Text='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Enabled=&quot;false&quot; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:CheckBox ID=&quot;chkDone&quot; runat=&quot;server&quot; Checked='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Text='&lt;%# Convert.ToBoolean(Eval(&quot;Done&quot;)) %&gt;' Enabled=&quot;true&quot;/&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField ShowHeader=&quot;False&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&quot;lnkEdit&quot; runat=&quot;server&quot; CausesValidation=&quot;False&quot; CommandName=&quot;Edit&quot; Text=&quot;Edit&quot; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:LinkButton ID=&quot;lnkUpdate&quot; runat=&quot;server&quot; CausesValidation=&quot;false&quot; CommandName=&quot;Update&quot; Text=&quot;Update&quot; /&gt; &lt;asp:LinkButton ID=&quot;lnkDelete&quot; runat=&quot;server&quot; CausesValidation=&quot;false&quot; CommandName=&quot;Delete&quot; OnClientClick='return confirm(&quot;Do you wish to delete selected data?&quot;);' Text=&quot;Delete&quot; /&gt; &lt;asp:LinkButton ID=&quot;lnkCancel&quot; runat=&quot;server&quot; CausesValidation=&quot;false&quot; CommandName=&quot;Cancel&quot; Text=&quot;Cancel&quot; /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; 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(&quot;chkDone&quot;); if (check.Checked) { session.Done = true; } else { session.Done = false; } context.SaveChanges(); } catch (Exception) { e.Cancel = true; } } }</pre> <p>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 ;)</p> 2011-10-25T11:27:31-04:00