locked
checkbox not updating in gridview based on entity datasource RRS feed

  • Question

  • User1224568436 posted

    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?

    Monday, October 24, 2011 6:19 AM

Answers

  • User1049502825 posted

    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....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2011 7:17 AM
  • User1224568436 posted

    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 ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 25, 2011 7:27 AM

All replies

  • User1049502825 posted

    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....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2011 7:17 AM
  • User1224568436 posted

    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.

    Monday, October 24, 2011 11:22 AM
  • User1224568436 posted

    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 ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 25, 2011 7:27 AM