Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to set these GridViews for updating by the admin. So since I have
a lot of employees and a lot of courses in each GridView, I think the best
way to update the GridView is to show the blank fields as checkboxes and when
the Admin wants to update the record of one of the employees, all what he
needs to do is just checking the checkboxes and click update button. I could
be able to show the empty fields as checkboxes but now I don't know how to
post the value of checking the checkbox to the database.

The scenario that I am looking for it is to make the system check every
checkbox in each GridView, if it is already checked before, leave it as it
is. If it was empty and it is just checked now, it should be added to the
database in the record of the employee. Therefore, the GridView will be updated with clicking the save button. My original code is:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
                    
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                        SelectCommandType="StoredProcedure" SelectCommand="kbiReport">
                                        <%--FilterExpression="[Division] like '{0}%' and [Organization] like '{1}%'">--%>
                            
                        <%--<FilterParameters>
                            <asp:ControlParameter ControlID="ddlDivision" Name="Division" 
                                                     PropertyName="SelectedValue" Type="String" />
                            <asp:ControlParameter ControlID="ddlOrganization" Name="Organization" 
                                                    PropertyName="SelectedValue" Type="String" />
                        </FilterParameters>--%>
    
                        <SelectParameters>
                            <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values 
                                of GroupID--%>
                            <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
                        </SelectParameters>
                    </asp:SqlDataSource>
    
                    <asp:GridView ID="GridView1" runat="server" 
                                    AllowSorting="True" 
                                    CellPadding="3" 
                                    DataSourceID="SqlDataSource1" 
                                    CssClass="mGrid"
                                    AlternatingRowStyle-CssClass="alt" 
                                    RowStyle-HorizontalAlign="Center" 
                                    OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound">
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <HeaderStyle Font-Bold = "true" ForeColor="Black"/> 
                        <Columns>
                            <asp:CommandField ShowSelectButton="True" />
                    
                            <%--<asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/>
                            </ItemTemplate>
                            </asp:TemplateField>--%>
                        </Columns>
                        <EditRowStyle BackColor="#999999" />
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </asp:GridView>
    
                </ItemTemplate>
            </asp:Repeater>
            
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                               ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                               SelectCommand="SELECT DISTINCT GroupID FROM courses">
            </asp:SqlDataSource>
    
            <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
            <br /><br />  


The Code-Behind:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                // Check if the cell vlaue = Yes
                // if it is Yes, the cell will be colored with Light Green
                if (c.Text.Equals("Yes"))
                {
                    c.BackColor = System.Drawing.Color.LightGreen;
                }
            }
        }

        // The following is for changing the color of headers in each GridView based on the value of the HiddenFild
        // BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database
        else if(e.Row.RowType == DataControlRowType.Header){
            switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
            {
                case "1":
                    for (int i = 5; i < e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
                    break;

                case "2":
                    for (int i = 5; i < e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
                    break;

                case "3":
                    for (int i = 5; i < e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
                    break;
            }
        }}


        //For inserting checkboxes inside all courses buttons
        protected void GridView1_DataBound(object sender, EventArgs e){
            GridView GridView1 = (GridView)sender;
            foreach (GridViewRow objRow in GridView1.Rows)
            {
                for (int i = 5; i < objRow.Cells.Count; i++){
                    CheckBox chkCheckBox = new CheckBox();
                    objRow.Cells[i].Controls.Add(chkCheckBox);
                    if (objRow.Cells[i].BackColor == System.Drawing.Color.LightGreen)
                        chkCheckBox.Checked = true;
                }

            }
        }


        //For updating the GridView (Save Button)
        protected void btnSave_Click(object sender, EventArgs e)
        {

        }


**UPDATE:**
I modified the btnSave_Click button to be as following:

C#
//For updating the GridView (Save Button)
        protected void btnSave_Click(object sender, EventArgs e)
        {
            GridView GridView1 = (GridView)sender;
            // Are there checked boxes?
            List CheckBoxList = new List();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                int courseid = (int)GridView1.DataKeys[i][0];
                CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
                if (checkBox.Checked)
                {
                    CheckBoxList.Add(courseid);
                }
            }
        }


But I got the following error:
**Sys.WebForms.PageRequestManagerServerErrorException: Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'**

I don't know why I got this error. Could anyone help me with this?
Posted

Try following:
C#
protected void btnSave_Click(object sender, EventArgs e)
   {

       foreach (RepeaterItem repeaterItem in Repeater1.Items)
       {
           GridView GridView1 = (GridView)repeaterItem.FindControl("GridView1");
           List CheckBoxList = new List();
           foreach (GridViewRow gvr in GridView1.Rows)
           {
               CheckBox checkBox = (CheckBox)gvr.FindControl("CheckBox");
                if (checkBox.Checked)
                {
                    CheckBoxList.Add(courseid);
                }
           }
       }
   }
 
Share this answer
 
Comments
matrix388 11-Dec-11 5:57am    
Since I am a new developer, could you please tell me what I should put inside (CheckBoxList.Add(............))?
Because I have a red color under the courseid
Hi
The error is pretty clear

protected void btnSave_Click(object sender, EventArgs e)
           {
               //GridView GridView1 = (GridView)sender;Here sender is button not the gridview
               // Are there checked boxes?
               List CheckBoxList = new List();
               for (int i = 0; i < GridView1.Rows.Count; i++)
               {
                   int courseid = (int)GridView1.DataKeys[i][0];
                   CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
                   if (checkBox.Checked)
                   {
                       CheckBoxList.Add(courseid);
                   }
               }
           }




Your gridview is in repeater. So try to find the gridview from repeater and do your logic.


Hope this helps.
 
Share this answer
 
v2
Comments
RaviRanjanKr 11-Dec-11 3:58am    
Always wrap your code in "Pre" tag.
sriman.ch 11-Dec-11 4:01am    
i wrapped it up pre tag but i don't know why it doesn't shown....anyway thanks for your suggestion...

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