Expand|Select|Wrap|Line Numbers
- protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GridView2.PageIndex = e.NewPageIndex;
- selectedCheckboxes();
- BindGrid();
- }
- private void BindGrid()
- {
- try
- {
- lblError.Visible = false;
- SqlCommand cmd2 = sqlcon.CreateCommand();
- cmd2.CommandText = "SELECT * FROM ImportData order by UserID";
- DataSet sds = new DataSet();
- SqlDataAdapter sda = new SqlDataAdapter(cmd2);
- sda.Fill(sds, "ImportData");
- GridView2.DataSource = sds.Tables["ImportData"].DefaultView;
- GridView2.DataBind();
- }
- catch (Exception ex)
- {
- lblError.Visible = true;
- lblError.Text = ex.Message;
- }
- }
- <asp:GridView
- ID="GridView2"
- OnPageIndexChanging="GridView2_PageIndexChanging"
- runat="server"
- AutoGenerateColumns="False"
- BorderStyle="Solid"
- CellPadding="4"
- DataKeyNames="UserID"
- BorderColor="Silver"
- BorderWidth="1px"
- Width="300px"
- AllowPaging="True"
- PageSize="4"
- ShowHeader="False">
- <Columns>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:CheckBox
- ID="chk1"
- runat="server"
- Text='<%#DataBinder.Eval( Container.DataItem, "UserID") +" "+DataBinder.Eval( Container.DataItem, "Name") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <HeaderStyle HorizontalAlign="Left" />
- </asp:GridView>
- <asp:HiddenField ID="hiddenCatIDs" runat="server" Visible="true"/>
- </div>
- protected void selectedCheckboxes()
- {
- string[] hiddenIDs = new string[] { };
- if (hiddenCatIDs.Value != string.Empty)
- {
- hiddenIDs = hiddenCatIDs.Value.Split(new char[] { '|' });
- }
- ArrayList arrIDs = new ArrayList();
- string CatID = "0";
- if (hiddenIDs.Length != 0)
- {
- arrIDs.AddRange(hiddenIDs);
- }
- CheckBox chk;
- foreach (GridViewRow rowItem in GridView2.Rows)
- {
- chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1"));
- CatID = GridView2.DataKeys[rowItem.RowIndex]["UserID"].ToString();
- if (chk.Checked)
- {
- Response.Write("chk.Checked in if :" + chk.Checked + "<br />");
- if (!arrIDs.Contains(CatID))
- {
- arrIDs.Add(CatID);
- }
- }
- else
- {
- Response.Write("chk.Checked in else :" + chk.Checked + "<br />");
- if (arrIDs.Contains(CatID))
- {
- arrIDs.Remove(CatID);
- }
- }
- }
- // ArrayList collection converted into string array.
- hiddenIDs = (string[])arrIDs.ToArray(typeof(String));
- // Join function of string array is used here to create a '|' separated string that can be stored in the hiddenCatIDs control.
- // HiddenField Control is used here to maintain the checkbox state while GridView paging.
- hiddenCatIDs.Value = string.Join("|", hiddenIDs);
- Response.Write(hiddenCatIDs.Value);
- }
- protected void btnMultipleRowDelete_Click(object sender, EventArgs e)
- {
- // Looping through all the rows in the GridView
- foreach (GridViewRow row in GridView1.Rows)
- {
- CheckBox checkbox = (CheckBox)row.FindControl("cbRows");
- //Check if the checkbox is checked.
- //value in the HtmlInputCheckBox's Value property is set as the //value of the delete command's parameter.
- if (checkbox.Checked)
- {
- // Retreive the User ID
- int UserID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
- // Pass the value of the selected User ID to the Delete //command.
- SqlDataSource1.DeleteParameters["UserID"].DefaultValue = UserID.ToString();
- SqlDataSource1.Delete();
- }
- }
- }