Click here to Skip to main content
16,006,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview with checkboxes, Now i want to get information about a specific row in the gridview when i check specific checkbox. It works when i want a single row but it reads one by one when i want a list of data. The button supose to return a list of Slot ID's when a header checkbox is checked.Check the code below

My gridview

XML
<asp:GridView ID="grvSlots" runat="server" CellPadding="4" ForeColor="#333333"
           GridLines="None" Height="162px" Width="188px" DataKeyNames="ID"
           onrowdatabound="grvSlots_RowDataBound" onrowcreated="grvSlots_RowCreated">
           <AlternatingRowStyle BackColor="White" />

           <EditRowStyle BackColor="#2461BF" />
           <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
           <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
           <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
           <RowStyle BackColor="#EFF3FB" />
           <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
           <SortedAscendingCellStyle BackColor="#F5F7FB" />
           <SortedAscendingHeaderStyle BackColor="#6D95E1" />
           <SortedDescendingCellStyle BackColor="#E9EBEF" />
           <SortedDescendingHeaderStyle BackColor="#4870BE" />

           <Columns>
           <asp:TemplateField HeaderText="select">
           <ItemTemplate>
               <asp:CheckBox ID="CheckBox1" runat="server" Width="50px" />

           </ItemTemplate>
          <HeaderTemplate>
          <asp:CheckBox ID="chkAll" runat="server" Text="Select"
           oncheckedchanged="chkAll_CheckedChanged" /></td>
          </HeaderTemplate>
           </asp:TemplateField>

           </Columns>
       </asp:GridView>




Code behind button

C#
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow rowItem in grvSlots.Rows)
        {

            CheckBox chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox1"));




            if (chk.Checked)
            {
              System.Windows.Forms.MessageBox.Show( grvSlots.DataKeys[rowItem.RowIndex]["ID"].ToString());
            }
        }

    }


Code in my client side for unchecking and checking the boxes

XML
<script type="text/javascript">
        function SelectAll(id) {
            var frm = document.forms[0];
            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox") {
                    frm.elements[i].checked = document.getElementById(id).checked;
                }
            }
        }
</script>



Code to check or uncheck the checkboxes

protected void grvSlots_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//adding an attribute for onclick event on the check box in the header
//and passing the ClientID of the Select All checkbox
((CheckBox)e.Row.FindControl("chkAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "')");
}
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
if (!Page.IsPostBack)
{
for (int i = 0; i < grvSlots.Rows.Count; i++)
{
CheckBox chk = (CheckBox)grvSlots.Rows[0].Cells[0].FindControl("chkAll");
if (chk != null)
{
if (chk.Checked)
{
id = grvSlots.Rows[0].Cells[0].Text;
sc.Add(id);
}
}
}
}
}


Below is how I populate my gridview

C#
public void loadGrvSlots()
    {
        DataTable dt = new DataTable();
        systemBusinessLayer = new BusinessLayer();

        dt = systemBusinessLayer.loadGrvSlots();
        grvSlots.DataSource = dt;
        grvSlots.DataBind();
    }



Method In my class to load gridview

public DataTable loadGrvSlots()
{
using (SqlConnection con = new SqlConnection(ConnString))
{
SqlCommand cmd = new SqlCommand("procAllGetTimeSlots", con);
cmd.CommandType = CommandType.StoredProcedure;

DataTable dTable = new DataTable("TimeSlots");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dTable);
return dTable;

}
}

Lastly my stored procedure

SQL
ALTER PROCEDURE [dbo].[procAllGetTimeSlots]
AS
SELECT     SlotName, ID
FROM         TimeSlots




Hope thats enough info guys
Posted

1 solution

for header Check box you can set the "auotpostback" property to true.
and at event get all data from grid view.
 
Share this answer
 

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