If we want to delete multiple users using Checkbox selection inside the GridView control with no paging can do easily. What is the case if there are multiple pages?
Introduction
If we want to delete multiple users using Checkbox selection
inside the GridView control with no paging can do easily. What is the case if there are
multiple pages?
Objective
In this article, I
will demonstrate how
to delete user
accounts
in different pages
by
maintain the state of the CheckBoxes that are
present inside the GridView
and how to
show the deleting users conformation list in a grid .
Using the code
Grid control to show users list:
<asp:GridView ID="gv_Dentistries"
runat="server"
AllowPaging="True"
OnPageIndexChanging="gv_Dentistries_PageIndexChanging" >
<PagerSettings Mode="NextPrevious" NextPageText="Next" PreviousPageText="Prev" />
<PagerStyle CssClass="trGridPager" /><RowStyle />
<Columns> <asp:TemplateField HeaderText="Last Name" SortExpression="LastName"> <ItemTemplate><asp:LinkButton ID="lnkLastname"
runat="server"
CommandName="cmdViewP"
Title="View
Patients"><%# Eval("LastName")
%></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemStyle Width="15%" />
<ItemTemplate>
<asp:Label ID="lbFirstName"
CommandName="ViewP"
runat="server"><%# Eval("FirstName”)%></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkAssign"
runat="server"
ToolTip ='<%# Eval("id")
%>' AutoPostBack="False" OnCheckedChanged="Check_Clicked" />
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Right" />
</asp:GridView>
Please add <asp:HiddenField ID="hiddenCatIDs"
runat="server"
/>
//Asp.net control to store checked user
ids
Server side code :
protected void
Check_Clicked(Object sender, EventArgs e)
{
selectedUsers();//Function to store
checked user id to hidden field
}
protected string selectedUsers()
{
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 gv_Dentistries.Rows)
{
chk = (CheckBox)(rowItem.Cells[0].FindControl("chkAssign"));
CatID =
gv_Dentistries.DataKeys[rowItem.RowIndex]["id"].ToString();
if
(chk.Checked && chk.Enabled)
{
if
(!arrIDs.Contains(CatID))
{
arrIDs.Add(CatID);
}
}
else
{
if
(arrIDs.Contains(CatID))
{
arrIDs.Remove(CatID);
}
}
}
hiddenIDs = (string[])arrIDs.ToArray(typeof(String));
hiddenCatIDs.Value = string.Join("|",
hiddenIDs);
return
hiddenCatIDs.Value;
}
protected void gv_Dentistries_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
StoreOldIds(gv_Dentistries); //Function
to store checked user ids
gv_Dentistries.PageIndex =
e.NewPageIndex;
populateDataGrid();//Function to
populate users list
GetIds(gv_Dentistries); //function to
get the stored ids
}
Store the Selected Ids:
private void
StoreOldIds(GridView grid) {
ArrayList
IDList = new ArrayList();
int
index = -1;
foreach
(GridViewRow row in
grid.Rows)
{
CheckBox
chk = (CheckBox)(row.Cells[0].FindControl("chkAssign"));
if
(chk.Checked)
{
index = Convert.ToInt32(chk.ToolTip);
}
else
{
index = Convert.ToInt32(chk.ToolTip);
}
bool
result = ((CheckBox)row.FindControl("chkAssign")).Checked;
// Check
in the Session
if
(Session["CHECKED_IDs"] != null)
IDList = (ArrayList)Session["CHECKED_IDs"];
if
(result)
{
if
(!IDList.Contains(index))
{
IDList.Add(index);
}
}
else
{
IDList.Remove(index);
}
}
if (IDList!=
null && IDList.Count > 0)
Session["CHECKED_IDs"]
= IDList;
}
Get the Selected IDs:
private void GetIds (GridView
grid)
{
if(Session["CHECKED_IDs"]!=null){
ArrayList
IDList = (ArrayList)Session["CHECKED_IDs "];
if
(IDList!= null && IDList.Count > 0)
{
foreach
(GridViewRow row in
grid.Rows)
{
CheckBox
chk = (CheckBox)(row.Cells[0].FindControl("chkAssign"));
int
index = Convert.ToInt32(chk.ToolTip);
if
(IDList.Contains(index))
{
CheckBox
myCheckBox = (CheckBox)row.FindControl("chkAssign");
myCheckBox.Checked = true;
}
}
}
}
}
As for here we maintained
the check boxes status while grid pages changing.
Page 1:
Page2:
Now we need to show list of
user selected in grid to take delete conforamtion.To show the list Call below function
in onclick event of delete button.
(gv_selected is grid used to
show the selected users list and keep it
in hidden state)
Populate the
selected users:
private void populateSelectedGrid()
{
string
chkBoxesSeleceted = selectedUsers();
string[]
myArray = new string[chkBoxesSeleceted.Length];
myArray = chkBoxesSeleceted.Split(new char[] { '|' });
DataTable
dt;
dt = users.getUsers(Convert.ToInt32(Session["userid"]));
DataSet
ds_row = new DataSet();
DataTable
dt3 = new DataTable("test");
DataView
dv;
ds_row.Tables.Add(dt3);
DataTable
dd = new DataTable();
dd.Columns.Add("FirstName");
dd.Columns.Add("LastName");
/*List of columns */
foreach
(string val in
myArray)
{
foreach
(DataRow dr in
dt.Rows)
{
if
(dr["id"].ToString() == val)
{
DataRow
d1 = dd.NewRow();
d1[0] = dr["FirstName"].ToString();
d1[1] = dr["LastName"].ToString();
/*Assign values to all columns */
dd.Rows.Add(d1);
dv = dd.DefaultView;
dv.Sort = "FirstName";
dt3 = dv.ToTable();
}
}
}
DataView dv2 =
dt3.DefaultView;
gv_selected.Visible = true;
gv_Dentistries.Visible = false;
gv_selected.DataSource = dv2;
gv_selected.DataBind();
}
}
Code to delete the users in the conformation
grid .
protected void imgSubmit_Click(object
sender, ImageClickEventArgs e)
{
populateSelectedGrid();
foreach
(GridViewRow row in
gv_selected.Rows)
{
//Code to delete the users in grid
}
}
Conclusion
In this article we learned about how to maintain checkboxes status in grid
using array list and session objects.
Please share your ideas related to this functionality …….
Reference
http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all