Maintaining State of CheckBoxes While Paging in a GridView Control
page 5 of 6
by Mohammad Azam
Feedback
Average Rating: 
Views (Total / Last 10 Days): 93203/ 158

Implementing GridView Page_IndexChangingEvent

The last thing that we need to implement is the Page_IndexChangingEvent of the GridView control. Page_IndexChangingEvent is fired whenever paging occurs. The implementation of this method is given below.

Listing 6: GridView Page_IndexChanging Event

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
  RememberOldValues();
  GridView1.PageIndex = e.NewPageIndex;
  BindData();
  RePopulateValues();
}

Analysis

The above method first calls RememberOldValues so that all primary keys of the checked CheckBoxes will be saved in a Session object. Next ,the page goes to the new GridView page, and finally, we populate the CheckBoxes using the RePopulateValues method.

You can see the effect in the screen shot below.

Figure 1: CheckBox Checked On First Page

Figure 2: CheckBox Checked On Second Page

Downloads

[Download Sample]


View Entire Article

User Comments

Title: Insert Value Multiple checkbox   
Name: DotNet
Date: 2012-10-17 4:02:22 AM
Comment:
hye any can help me to findout.. howto insert multiple selected checkbox value to database using asp..
Title: DDSF   
Name: SADS
Date: 2012-10-13 12:44:13 AM
Comment:
SDA
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Vadim
Date: 2012-09-28 7:46:44 AM
Comment:
Thanks very match. It's very usefull post.
Title: dsff   
Name: sdfd
Date: 2012-08-24 4:36:40 AM
Comment:
dsfsdfd
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: sachin jain
Date: 2012-07-18 3:09:56 AM
Comment:
Thanx......
Title: check boxes selected items go on another page in asp.net   
Name: Rajesh
Date: 2012-06-01 4:18:15 AM
Comment:
Kindly check this url http://www.jcbpartsindia.com/p25.aspx

i want to select check boxes then click on send inquiry button and go all selected items on inquiry page only part no and description not image .

I am using ASP.Net 3.5 c#.
plz provide me solution i need in urgent bases.

plz mail me solution on my mail ID (rajeshk997@gmail.com)
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Zin
Date: 2012-05-16 11:10:12 PM
Comment:
Thanks a lot for this post!
Very Helpful!
Title: hi   
Name: Name
Date: 2012-04-27 5:58:26 AM
Comment:
Hi,
I am Getting below error
I am using sql datasource

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 783: Dim index As Integer = -1
Line 784: For Each gvrow As GridViewRow In gridview.Rows
Line 785: index = Convert.ToInt32(gridview.DataKeys(gvrow.RowIndex).Value)
Line 786: Dim result As Boolean = DirectCast(gvrow.FindControl("chkStatus"), CheckBox).Checked
Line 787:
Title: Index was out of range.   
Name: Sandy
Date: 2012-04-10 9:57:01 AM
Comment:
Hi,
I am Getting below error
I am using sql datasource

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 783: Dim index As Integer = -1
Line 784: For Each gvrow As GridViewRow In gridview.Rows
Line 785: index = Convert.ToInt32(gridview.DataKeys(gvrow.RowIndex).Value)
Line 786: Dim result As Boolean = DirectCast(gvrow.FindControl("chkStatus"), CheckBox).Checked
Line 787:
Title: CHECKED ITEMS   
Name: Rahu
Date: 2012-01-09 9:08:39 AM
Comment:
Can you describe,wat do u mean by CHECKED_ITEMS in seccion..
Thank you
Title: In visual studio2003   
Name: Guru
Date: 2011-11-08 7:08:19 AM
Comment:
hi, am using VS 2003 environment.. so i cant use this coding such as
foreach (GridViewRow row in grdSystemVariables.Rows)
{
index = (int)grdSystemVariables.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;


can any one tell the code?
Title: another way to solve the problem   
Name: Starlyn
Date: 2011-09-01 3:54:11 PM
Comment:
I leave here another way to keep the selection of the check .... if you need more information let me know.

private void guardarSeleccion()
{
string que = string.Empty;
Boolean ck;
// n = ver.NewRow();
int ind = GridView2.PageIndex;

//Limpiar Tabla/////////////////////////////////////////////////////
if (gri.Rows.Count > 0)
{
for (int r = 0; r < gri.Rows.Count; r++)
{

if (gri.Rows[r].Cells[1].Text == Convert.ToString(ind))
{

// ver.Rows[r].Delete();
gri.Rows[r].Cells[0].Text = "";
gri.Rows[r].Cells[1].Text = "";
gri.Rows[r].Cells[2].Text = "";

}
}

}

//Fin de Limpiar Tabla//////////////////////////////////////////

for (int l = 0; l < GridView2.Rows.Count; l++)
{
n = ver.NewRow();
CheckBox cb = (CheckBox)GridView2.Rows[l].Cells[12].FindControl("CheckBox1");
if (cb.Checked == true)
{
ck = true;
}
else
{
ck = false;
}
n["1"] = l;//Filaa////////////////////
n["2"] = ind;//index de la pagina/////
n["3"] = ck;//estado del CheckBox/////

ver.Rows.Add(n);
}
// ver.Rows.Add(n);


gri.DataSource = ver;
gri.DataBind();

}


private void EstablecerPosicion()
{
int ind = GridView2.PageIndex;
int row ;
bool valor ;

if (gri.Rows.Count > 0)
{
for (int r = 0; r < gri.Rows.Count; r++)
{

if (gri.Rows[r].Cells[1].Text == Convert.ToString(ind))
{
row = Convert.ToInt32( gri
Title: Index was out of range.   
Name: Timsen
Date: 2011-08-20 9:37:55 PM
Comment:
Hi,
code looks great but would be even better when there wouldn't be that annoying error :)

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Any working solutions??
Thanks!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: somitha
Date: 2011-05-06 3:04:12 AM
Comment:
Really Awesome article .. Thank u so much
Title: Display gridview selected data after selecting item in dropdownlist.   
Name: Pankaj Kishore
Date: 2011-04-21 9:34:55 AM
Comment:
I am developing ASP.net applicaion. In my page there is one dropdownlist and one gridview control inside gridview there is one checboxlist.User check the gridview row and press save button data save in database.its ok now my question is i want to display data in gridview when user select any item in dropdownlist display related item in gridview. I am using ASP.Net 2.0 c#.
plz provide me solution i need in urgent bases.

plz mail me solution on my mail ID (priyampankaj@gmail.com)
Title: Index was out of range.   
Name: Khan Mohd Faizan
Date: 2011-03-16 1:37:41 AM
Comment:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
plz provide me solution i need in urgent bases

plz mail me solution on my mail ID (khanfaizan22@gmail.com)
Title: saving checked rows for all pages in gridview control   
Name: yogesh
Date: 2011-03-07 4:28:29 PM
Comment:
nice and helpful, also I am looking for the one where i can save all the checked rows of different pages in the gridview c# code.
mail me y.kollu@gmail.com
Title: Stypid   
Name: Omega
Date: 2011-02-19 2:07:43 PM
Comment:
Absolutly no need using BindData method. Method RePopulateValues must be called from hendler of PreRender event for gridview
Title: Congratulations   
Name: Dinho
Date: 2011-02-03 8:05:34 AM
Comment:
Congratulations

Good work, congratulations for example.

Thanks
Title: My Problem   
Name: sunnyjagtap11@gmail.com
Date: 2011-01-17 4:37:27 AM
Comment:
i maintain counter in Javascript on childclick,
but onload it gets clear,
i want to restrict user from selecting more than 3 items.
How can i do that in Javascript??
Title: Thanx a Tonn   
Name: Rizwan
Date: 2010-12-06 1:13:16 AM
Comment:
Thanx Mate
Title: thanx!   
Name: casinos en línea
Date: 2010-10-25 11:17:38 AM
Comment:
thanx for that Query!
Title: Thx   
Name: Avinash
Date: 2010-10-13 11:17:51 AM
Comment:
Thank you so much aliayaaaaaaaaa
Title: Thank you Mr.Azam   
Name: Vishal Singh
Date: 2010-09-28 6:10:36 AM
Comment:
your article was a great help for me and i hope also for the asp people having problems regarding this aspect of the gridview paging. once again thank you and keep going
Title: En caso que llenes el GridView Manualmente   
Name: Keylin
Date: 2010-08-25 5:05:34 PM
Comment:
Hola, después de 16 horas de estar batallando con la paginación porque me borraba las selecciones de los checkbox encontré este sitio y es excelente, ya pude.

Quiero decirles que en caso que llenen el GridView de manera manual(no por wizard), no basta con solamente llamar al GridView.BindData() en el metodo GridView_PageIndexChanging, sino que deben de volver a asignar el datasource para asignarselo al GridView y luego hacer el DataBind().

Espero que esto les sirva! Gracias
Title: http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all   
Name: http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.2
Date: 2010-05-19 5:03:05 AM
Comment:
Please also note that the paging is enabled in the GridView control, and the DataKeyNames property has been set to "CategoryID," since it’s the primary key in the database
. The PageSize property has been set to 5, which means that after one page of the GridView can only contain 5 records/rows. The ItemTemplate tag contains the CheckBox whose ID is "CheckBox1," which means that CheckBoxes will be displayed for the last column.
Title: gridview paging   
Name: Anil Thakur
Date: 2010-05-18 12:06:18 PM
Comment:
It is very usefull article. It helf lot of programmer. thanks a lot.
Title: gridview paging   
Name: Nadeem
Date: 2010-05-04 5:21:44 PM
Comment:
It is really excellent and generic code. I appreciate this great effort.
Title: gridview paging problem   
Name: charan
Date: 2010-04-12 2:31:31 AM
Comment:
hi iam using asp.net2.0 with c#

In the gridview 1st column is checkbox,in my gridview iam using paging , iam dispalying data at page load, and iam usin data from view so there is no primary key

when user selects the check box that records are stored in database.
but when i selects the checkbox at 1st page then it is
storing all the records but when i selects the 1st page records as well as 2nd page records
then it is storing only second page records .


iwant to store all the records what ever user has selects the checkbox.

can you give me example which helps me
Title: Thanks a Lot you save my Life   
Name: Mohammed
Date: 2010-04-08 1:50:45 AM
Comment:
Thanks Mohammed Azam, U r work saved me from lot of troubles
Title: Excellent   
Name: Milton
Date: 2010-04-06 5:36:16 PM
Comment:
Hello, that job was well done, thanks a lot!
Title: Great It works fine....   
Name: Pankaj Ghodake
Date: 2010-04-06 6:52:22 AM
Comment:
Hi Mohammad
Truly THANKS you solve my big problem Thanks....
Regards
PANKAJ
Title: Used same for the text box with few modification   
Name: Sumit
Date: 2010-03-15 2:03:42 AM
Comment:
Hi Mohammad, ihave use same code for the text box with few modifications. but at the time of repopulating the values i get nothing in text box. Also no error occured at that time.
It' would be highly appreciable if you suggest me on this on my mail.
sumit.avasthi@gamil.com
Title: Thanks   
Name: Sumit
Date: 2010-03-15 1:38:32 AM
Comment:
Thanks Mohammad it's really very helpful
Title: changes to add   
Name: bala
Date: 2010-03-08 2:31:02 AM
Comment:
very nice article!
Missing quotes:
Session[CHECKED_ITEMS] to Session["CHECKED_ITEMS"]
call BindData() in page load.(for beginners)

Cheers,
balaneharavi@gmail.com
Title: GridView+pageindexChanging+checkbox   
Name: Claire
Date: 2010-03-04 5:42:55 AM
Comment:
This was really helpful. Thanks a lot
Title: Need the same in case of column sorting..   
Name: Ramesh
Date: 2010-01-28 12:21:30 AM
Comment:
Hi I need the same logic in case of sorting different columns in the grid view. I do not have paging.
Title: Continued...   
Name: Rich
Date: 2010-01-22 6:27:20 AM
Comment:
protected void grdSystemVariables_DataBound(object sender, EventArgs e)
{
RePopulateValues();
}
Title: This works   
Name: Rich
Date: 2010-01-22 6:26:37 AM
Comment:
I have spent all morning getting this to work and the following code works...

private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in grdSystemVariables.Rows)
{
index = (int)grdSystemVariables.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;

// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}

private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in grdSystemVariables.Rows)
{
int index = (int)grdSystemVariables.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
myCheckBox.Checked = true;
}
}
}
}

protected void grdSystemVariables_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
grdSystemVariables.PageIndex = e.NewPageIndex;
}

protected void grdSystemVariables_DataBound(object sen
Title: GridView+pageindexChanging+checkbox   
Name: Nandu Patil
Date: 2010-01-12 12:39:06 AM
Comment:
Nice one.


Very Very Very Very Very Very...... Thanks
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Bhavin
Date: 2009-10-10 5:23:23 AM
Comment:
Haiii nice one but pls send articel in VB.net
Title: Works like a charm!   
Name: Matt M
Date: 2009-10-07 9:51:46 AM
Comment:
You rock, man! This made my life much easier.
Title: Thanks.....Mohammad   
Name: Syed
Date: 2009-09-28 7:18:26 AM
Comment:
It's working fine...thanks for your demonstration
Title: Index was out of range   
Name: Balu
Date: 2009-09-01 10:30:19 AM
Comment:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I'm getting this error I'm using your code in sharepoint webpart. In this webpart I have gridview with paging.

Can anyone help me with this issue. Thanks in advance.
Title: Index was out of range.   
Name: LNACELLI
Date: 2009-08-18 4:14:14 PM
Comment:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

In the gridview properties, make sure that you set the property DataKeyNames. That's all you need to do.
Title: index = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);   
Name: Pankaj
Date: 2009-08-08 2:18:05 AM
Comment:
I amgetting an error in this
[ index = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);]
Line.
Error::
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Please help me, on my Email ID( pssinghpankaj4@gmail.com )
Title: result = ((CheckBox)row.FindControl("CheckBox1")).Checked; always return false even I did check   
Name: Allen
Date: 2009-07-19 1:08:13 AM
Comment:
I have problem with RememberOldValues()function, bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;
always return false even though I did select the check box. Do you know why I have this kind of problem?
Title: thanx   
Name: deep
Date: 2009-07-13 3:16:08 AM
Comment:
Very helpful...Thanks a lot!
Title: In place of checkbox i need four radio buttons   
Name: param yellale
Date: 2009-07-10 9:47:14 AM
Comment:
its really nice code..
In place of checkbox i need four radio buttons for quizing applicaion. please suggest me...
my email id yellaleparam@gmail.com
Title: Second time back to the Page   
Name: GeeTee
Date: 2009-06-24 8:39:18 PM
Comment:
This works the first time I go back to a page but if I return to the page a second time all the boxes are unchecked again. Any ideas why?
Title: Slight adjustements and it works great 2   
Name: Ricpue - NYC (cont)
Date: 2009-06-01 11:58:28 AM
Comment:
also copy the code below...

protected void gdvallreports_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues(gdvallreports, "CheckBoxSelect");
}

protected void gdvallreports_DataBound(object sender, EventArgs e)
{
RePopulateValues(gdvallreports, "CheckBoxSelect");
}
Title: Slight adjustements and it works great   
Name: Ricpue - NYC
Date: 2009-06-01 11:56:57 AM
Comment:
Great job Mohammad thanks!

I'm in asp.net 2.0 and after doing the changes as is below the code work great.


private void RememberOldValues(GridView gridview, string checkboxname)
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in gridview.Rows)
{
index = (int)gridview.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl(checkboxname)).Checked;

// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}

private void RePopulateValues(GridView gridview, string checkboxname)
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in gridview.Rows)
{
int index = (int)gridview.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox checkbox = (CheckBox)row.FindControl(checkboxname);
checkbox.Checked = true;
row.BackColor = Color.FromName("#FFAA63");
}
}
}
}


protected void gdvallreports_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues(gdvallreport
Title: Drawback   
Name: vineet kumar
Date: 2009-04-21 8:45:51 AM
Comment:
Your article is very good...
but in case where we are applying sorting also then it will not work......................
Title: Its working but still some problem...   
Name: Happy Agarwal
Date: 2009-04-09 2:14:31 AM
Comment:
Hi..
Its working properly but if i want to sum up all the checked values than it doesnt work. when i do paging it forget all the checked values..
Title: Doesnt work with database other than SQL Server   
Name: RahulGCT
Date: 2009-03-30 4:16:15 AM
Comment:
Hey Every1

Did any try this sample code with a database other than sqlserver?????
I tried it with a MSAccess database and strangely enough it doesn't work!!!!
Any clues....
Title: SQLDATASOURCE   
Name: Hugo
Date: 2009-03-19 5:23:32 AM
Comment:
Hi,
I'm using a sqldatasource in the gridview, and with your code it doesn't work, the girdview keeps refreshing and all the checked values disappear.
Thanks for help.
Title: Control Not Found   
Name: Prateek
Date: 2009-03-09 11:46:19 AM
Comment:
I am getting error message that control not found for the line ((CheckBox)row.FindControl("CheckBox1")) in RememberOldValue()
Title: CHECKED_ITEM   
Name: Przemek
Date: 2009-03-03 4:30:57 AM
Comment:
@AMAR

Add this line at the top:
private const string CHECKED_ITEMS = "CheckedItems";
It contains the session key.
Title: What is CHECKED_ITEM   
Name: AMAR
Date: 2009-03-02 1:08:08 PM
Comment:
Hi,

I saw your article its very good , I tring this code its giving CHECKED_ITEM does not exits this context.
Title: GridView checkboxes and UpdatePanel   
Name: Przemek
Date: 2009-03-02 9:40:05 AM
Comment:
RePopulateValues() doesn't seem to work for me. According to the debugger, (CheckBox)row.FindControl("SampleCheckBox") finds the appropriate control, but when I change the page and come back the CheckBox isn't checked. The GridView is inside the UpdatePanel, could this be an obstacle here? If yes, how to modify the code to work inside the UpdatePanel?
Title: Good article   
Name: Pawan Kumar
Date: 2009-02-07 3:18:00 AM
Comment:
HI
I found the article on this website very informative for all type of developers

Thanks
by Pawan
Title: Instead of checkbox   
Name: Ullas
Date: 2009-01-29 3:25:23 AM
Comment:
Instead of checkbox, if i'm having more than one radio buttons, should i use different viewstates?
Title: GridView1_PageIndexChanging()   
Name: Ms. Leah
Date: 2009-01-16 2:37:27 PM
Comment:
Actually the RePopulateValues() method did work for me in the PageIndexChanging event... I placed it in the GridView1_RowCreated event and it worked like a charm!!
Title: Regarding CHECKED_ITEMS does not exist:   
Name: Ms. Leah
Date: 2009-01-16 2:35:05 PM
Comment:
Add this line at the top:
private const string CHECKED_ITEMS = "CheckedItems";

Regarding CHECKED_ITEMS does not exist:
Title: Specified cast is not valid.   
Name: Ms. Leah
Date: 2009-01-16 2:33:31 PM
Comment:
I m getting error :Specified cast is not valid.

@ index = (int)GridView1.DataKeys[row.RowIndex].Value;
FIX: index = int.Parse(GridView1.DataKeys[row.RowIndex].Value.toString())
Title: Index was out of range. Must be non-negative and less than the size of the collection.   
Name: Ronnie Overby
Date: 2009-01-05 11:37:46 AM
Comment:
I too was receiving this error: Index was out of range. Must be non-negative and less than the size of the collection.

This doesn't have anything to do with converting to an integer as some comments have suggested, although that does solve a different problem that some of the commenters have had.

For me, the problem was that the Gridview's DataKeys collection was empty because I was using an ObjectDataSource to populate my GridView. So, because I was not binding in the PageIndexChanging event handler, I did not have any DataKeys at this point.

So, I left the RememberOldValues method into GridView.PageIndexChanging event handler and moved the RePopulateValues method into the GridView.DataBound event handler.

This fixed the problem for me. Hope this helps.
Title: Misc stuff   
Name: A. Soong
Date: 2008-12-26 3:20:04 PM
Comment:
First of all, thank you AzamSharp for posting. You really saved my hours of time.

Regarding Repopulating checkboxes in GridView solution posted by Anonymous on 8/13/2006 8:30:45 PM: Brilliant! Thank you for this post whoever you are....

Regarding CHECKED_ITEMS does not exist: Download the sample, it has more code that is not covered in the article. Specifically, this line: private const string CHECKED_ITEMS = "CheckedItems";

Regarding Specified cast is not valid: Most likely, its because the DataKeyNames for the gridview is not of type int like in the sample. That was my issue. Cast it to the right type.

Best of luck...
Title: Specified cast is not valid.   
Name: satya
Date: 2008-12-24 3:57:57 AM
Comment:
I m getting error :Specified cast is not valid.

@ index = (int)GridView1.DataKeys[row.RowIndex].Value;

????????????????? using vs 2008

//use this line
@ index = convert.toint32(GridView1.DataKeys[row.RowIndex].Value);
Title: Thanks   
Name: Suresh
Date: 2008-12-14 2:05:18 AM
Comment:
Hi
Thaks for the Coding provided
Title: GridView1_PageIndexChanging()   
Name: Ramakrishna
Date: 2008-12-04 4:56:17 AM
Comment:
RememberOldValues(); is calling repeatedly in
GridView1_PageIndexChanging()please help me.
Title: selectall rows across pagination   
Name: micky
Date: 2008-11-25 10:49:41 PM
Comment:
hello,

Can anyone tell me how to set all pages selected ,when selectall link in headerrow is checked.its urgent.
Title: Specified cast is not valid.   
Name: Sunil
Date: 2008-11-24 4:54:09 AM
Comment:
I m getting error :Specified cast is not valid.

@ index = (int)GridView1.DataKeys[row.RowIndex].Value;

????????????????? using vs 2008
Title: using combobox   
Name: manojmeeurt
Date: 2008-11-15 12:01:41 AM
Comment:
hi,plz help me .
suppose i have one combobox .and two checkbox and i want to del,update,save,by the help of this combobox.and checkbox work for this when i will click chek all then all value are checked and when i click to none checkbox then uncheck all the record...and the work of combobox when i click the combox the box show to me update the record,del,save...plz help me my id is

manojmeerut@rocketmail.com
Title: Using Textbos   
Name: Rachana
Date: 2008-10-31 3:40:47 AM
Comment:
Hi,This article is really good and it worked perfectly with the checkbox.I have the textbox also in the grid along with the checkbox.How to maintain the status of the Textbox along with the checkbox...Please help and reply to this mail id--rachanatj2004@yahoo.co.in

Regards
Rachna
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: satyanarayana
Date: 2008-10-29 9:14:29 AM
Comment:
Hi guy,
Really it's excellent and everything is working fine.
Thanks for u r great work.

Regards
D.Satyanarayana
satyadnet@gmail.com
Title: Issue   
Name: Man
Date: 2008-10-27 1:46:43 AM
Comment:
Comment:
I tried to implement your code in of my webpages
But its popping an error saying

The name 'CHECKED_ITEMS' Does not exist
How can i get rid of this one
Please Let me know ?
Title: CheckBox status after Postack in GridView   
Name: Harish KV
Date: 2008-09-29 5:20:41 AM
Comment:
I was simply Impressed...

Thanks
Title: I need the same code for TextBox   
Name: Suresh
Date: 2008-09-12 8:42:02 AM
Comment:
Hello!! Any one plz help me with the same sort of code for the TextBox. Fighting like anything for past two days.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Carlos Cuapio
Date: 2008-09-10 10:12:51 AM
Comment:
Excellent! Thanks for the article. you are great

Greetings from Mexico
Title: nice coding   
Name: Shiva
Date: 2008-08-25 1:13:43 AM
Comment:
Cod is working Fine thanks...keep it up:)
Title: For those who love to code in VB   
Name: Fred
Date: 2008-07-21 1:38:11 PM
Comment:
Instead of using <> operator in the if condition (IN VB)for comparing the arraylist

use If not arraylist is nothing Then

This solved my problem
Title: Issue   
Name: Fred
Date: 2008-07-17 4:15:19 PM
Comment:
I tried to implement your code in of my webpages
But its popping an error saying

The name 'CHECKED_ITEMS' Does not exist
How can i get rid of this one
Please Let me know ?
Title: Question...................   
Name: Lyka
Date: 2008-06-27 4:29:59 AM
Comment:
Error3. The name 'CHECKED_ITEMS' does not exist in the current context. What does this mean?...
Title: Thanks   
Name: Gabriel
Date: 2008-06-12 11:28:41 AM
Comment:
Excelente. It works very well. thanks

Gabriel From Argentina
Title: the solution   
Name: dan
Date: 2008-06-02 9:09:04 AM
Comment:
this solution of using session is very bad because if there are alot of records inside the gridvie the server will be busy

the best way to do this solution is with HiddenField
Title: Index was out of range. Must be non-negative and less than the size of the collection   
Name: GreggSie
Date: 2008-05-23 8:06:43 AM
Comment:
There needs to be values in DataKeyNames property
Title: Index was out of range. Must be non-negative and less than the size of the collection   
Name: geeram
Date: 2008-04-23 10:36:07 PM
Comment:
getting error : Index was out of range. Must be non-negative and less than the size of the collection.
in line no.

index = (int)gdview1.DataKeys[row.RowIndex].Value;

i changed this line to
Index = Convert.ToInt32 (gdview1.DataKeys [row.RowIndex]. Values;

still i am getting the same error.

can anyone plz help
Title: I need save the CategoryName into array.   
Name: Rodrigo
Date: 2008-04-18 4:48:54 PM
Comment:
Hello, Thanks for your article, I need save the categoryname into array, how do I do that?
Title: It is working now   
Name: sameer ali khan
Date: 2008-04-16 6:46:08 AM
Comment:
Hi,
I had just posted the query to you.Please ignore that.I was able to rectify my problem.Now it is working.Thankyou.
Title: Cannot mantain state of checkboxes while using custom paging   
Name: sameer ali khan
Date: 2008-04-16 6:32:45 AM
Comment:
Hi,
I am using custom paging in gridview and i took help from your example.It is working fine in gridview inbuilt paging but when custom paging, i cannot mantain state of page. Can you please give me any idea regarding it?
Title: a little fix   
Name: Maralbust
Date: 2008-04-08 10:21:54 AM
Comment:
Hello everybody I download the code, but I got the same mistake that other ones, to fix the code to do the following:

Index = (int) GridView1.DataKeys [row.RowIndex]. Value;
For
Index = Convert.ToInt32 (GridView1.DataKeys [row.RowIndex]. Value);

In both functions and RePopulateValues and rememberoldvalues and that is, the code running :) I hope will help them.

Greetings from Chile
Title: error : Index was out of range. Must be non-negative and less than the size of the collection.   
Name: Ram
Date: 2008-03-12 3:40:56 AM
Comment:
getting error : Index was out of range. Must be non-negative and less than the size of the collection.
in line no.

index = (int)gdviewPolicyDetails.DataKeys[row.RowIndex].Value;
Title: error : Index was out of range. Must be non-negative and less than the size of the collection.   
Name: salon
Date: 2008-03-11 4:53:15 AM
Comment:
getting error : Index was out of range. Must be non-negative and less than the size of the collection.
in line no.

index = (int)gdviewPolicyDetails.DataKeys[row.RowIndex].Value;
I have changed as
index = Convert.ToInt32(gdviewPolicyDetails.DataKeys[row.RowIndex].Value);
but still getting the same error
Title: Checkbox Problem   
Name: Poonam
Date: 2008-03-03 2:38:09 AM
Comment:
For Each row As GridViewRow In GridView2.Rows
Dim ch As New System.Web.UI.WebControls.CheckBox
ch = (row.FindControl("CheckBox1"))
If (ch.Checked = True) Then
Dim s As String = GridView2.SelectedRow.Cells(1).ToString()
End If
Next

''I am getting value false eventhough checkbox is checked
i am not able to understand why can u plz mail me at
poonamwalimbe@yahoo.co.in
Title: testing for > 0   
Name: Ben
Date: 2008-01-02 9:57:23 AM
Comment:
I don't understand why you are testing for categoryIDList.Count > 0 before updating Session[CHECKED_ITEMS]... this seems to be a bug.

Consider what happens if Session[CHECKED_ITEMS] currently contains a categoryIDList containing a few items. Then the user goes and unchecks everything, so all items will be removed from categoryIDList and categoryIDList.Count will end up being 0. As a result, Session[CHECKED_ITEMS] will not be updated, and the previously selected items will remain stored in Session[CHECKED_ITEMS].
Title: checkbox   
Name: amarnath reddy
Date: 2007-12-28 6:07:56 AM
Comment:
This is very good article.
Title: remember Textbox   
Name: Helen
Date: 2007-11-28 2:05:30 PM
Comment:
This is an excellent article. Thank you. Now I'm trying to do the same thing but with textbox values. i'm unable to make it work.. could anyone show sample code to help me out?
Title: checkbox   
Name: mobina
Date: 2007-11-28 7:36:55 AM
Comment:
i prepared a grid view for displaying my contents of table. I had considered for all the rows a check box..with out using the edit template ..if i need need to delete a particular row that in had checked recently..how can i go with
Title: Check   
Name: saiprasad.teegala@valuelabs,net
Date: 2007-09-19 5:54:16 AM
Comment:
hi sriLakshmi ,in page load
write if(!IsPostBack)
{
write the entire logic here

}
Title: My version with VStudio 2005   
Name: Daniel Perez
Date: 2007-09-13 11:57:51 AM
Comment:
Can you mail me? I have other version that works,
because your version did not work for me.
I tried to post it here, but I got and error.
bye,
dany7487@yahoo.com
Title: What if I am creating the grid dynamically   
Name: Syed Anas Razvi
Date: 2007-08-31 6:01:21 AM
Comment:
Hello All,
I am OK with the code... The effort is appreciable..
My scenario is like this...
I am creating my grid at runtime and upon Clicking of a button, I wanted RememberOldValues() to be called before postback happens because of the button click... Where should I call the RememberOldValues().
Title: need the equivalent code for datagrid   
Name: winnie
Date: 2007-08-30 1:14:06 AM
Comment:
hi
i need the equivalent code for datagrid

index = (int) GridView1.DataKeys[row.RowIndex].Value;
Title: Doesn't get the true value for checked CheckBox   
Name: Srilakshmi
Date: 2007-08-11 5:55:12 AM
Comment:
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;
im getting "false" value for even checked CheckBox, can't find anything wrong. Please help!
please send to srilakshmi15@gmail.com

Note: There's no column for checkbox in the database, i want to use checkbox to select a value.
Title: Code in VB.NET (2/2)   
Name: Canellaf
Date: 2007-07-17 11:36:30 AM
Comment:
Protected Sub gv_cont_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_cont.DataBound
RePopulateValues()
End Sub

Protected Sub gv_cont_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv_cont.PageIndexChanging
RememberOldValues()
gv_cont.PageIndex = e.NewPageIndex
gv_cont.DataBind()
End Sub
Title: Code in VB.NET   
Name: Canellaf
Date: 2007-07-17 11:35:44 AM
Comment:
Thanks a lot.

Working with VB.NET (I've also changed some names for my specific project)

Private Sub RememberOldValues()

Dim index As String
Dim result As Boolean
Dim prs_login_List As New ArrayList()
Dim gvRow As GridViewRow
Dim CheckBox As System.Web.UI.WebControls.CheckBox

For Each gvRow In gv_cont.Rows

index = gv_cont.DataKeys(gvRow.RowIndex).Value
CheckBox = gvRow.FindControl("CheckBox1")
result = CheckBox.Checked

' Check in the Session
If Not IsNothing(Session("CHECKED_ITEMS")) Then
prs_login_List = Session("CHECKED_ITEMS")
End If

If result Then
If Not prs_login_List.Contains(index) Then
prs_login_List.Add(index)

End If

Else
prs_login_List.Remove(index)

End If

Next
If Not IsNothing(prs_login_List) Then
If prs_login_List.Count > 0 Then
Session("CHECKED_ITEMS") = prs_login_List
End If
End If
End Sub

Private Sub RePopulateValues()

Dim index As String
Dim prs_login_List As New ArrayList()
Dim gvRow As GridViewRow
Dim CheckBox As System.Web.UI.WebControls.CheckBox
prs_login_List = Session("CHECKED_ITEMS")

If Not IsNothing(prs_login_List) Then
If prs_login_List.Count > 0 Then

For Each gvRow In gv_cont.Rows
index = gv_cont.DataKeys(gvRow.RowIndex).Value

If prs_login_List.Contains(index) Then

CheckBox = gvRow.FindControl("CheckBox1")
CheckBox.Checked = True
End If
Next
End If
End If

End Sub

Protected Sub gv_cont_DataBound(ByVal sender As Object, ByVal e
Title: hi..............   
Name: lopa
Date: 2007-06-04 7:18:20 AM
Comment:
foreach (GridViewRow row in GridView1.Rows).
im workin in vb.net replacing of this code line is " For Each row As GridViewRow In DG_Persons.rows". im gettin error line in DG_persons.rows.
plz.........seggest me
Title: hi...can i have this code in vb.net   
Name: lopa
Date: 2007-06-04 3:24:24 AM
Comment:
hi..........i want to do same job in ASP.net with vb language. If yes...plz pescribe me, how to do?b'cz i m tryin in vb. bt it's nt worked out.

Thank you

Waitin,
lopashree@rediffmail.com
Title: hai it is very useful   
Name: senthil kumar
Date: 2007-06-02 6:32:22 AM
Comment:
Hai
it is very useful
Title: Keys is not int   
Name: Akram
Date: 2007-06-01 10:48:45 PM
Comment:
Hi
What if the data key was not int (e.g. string), or more than 1 column?!!
how to do it then?
Thanks
Title: Error   
Name: Manju
Date: 2007-05-17 2:55:27 AM
Comment:
error : Index was out of range. Must be non-negative and less than the size of the collection.

private void rememberoldvalues()
{
ArrayList emplist = new ArrayList();
int index = -1;

foreach (GridViewRow row in GridView1.Rows)
{

index = (int)GridView1.DataKeys[2].Value;
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;

// Check in the Session
if (Session[CHECKED_ITEMS] != null)
emplist = (ArrayList)Session[CHECKED_ITEMS];
if (result)
{
if (!emplist.Contains(index))
emplist.Add(index);
}
else
emplist.Remove(index);
}
if (emplist != null && emplist.Count > 0)
Session[CHECKED_ITEMS] = emplist;


}
Title: Fix for the repopulation problem   
Name: Udit
Date: 2007-05-15 11:11:56 AM
Comment:
The reason that the re-population of the checkboxes doesn't work is, as mentioned in a post below, that the DataBind runs AFTER the PageIndexChanged has run.

The solution to this is to simply call the RePopulateValues() in the OnDataBound event, and it work perfectly.
Title: Using RadioButtonList   
Name: Javed Bux
Date: 2007-02-21 2:04:19 AM
Comment:
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;

Label lbl = (Label)row.FindControl("Label1");
//int index = Convert.ToInt32(lbl.Text);

if (categoryIDList.Contains(index))
{
RadioButtonList RD = (RadioButtonList)row.FindControl("RadioButtonList1");
RD.Items[index].Selected = true; // error in this line


//CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
// myCheckBox.Checked = true;
}
}

}

}
Title: Repopulating RadioButtonList in GridView   
Name: Javed Bux
Date: 2007-02-21 2:02:31 AM
Comment:
Thanks for this Article... it is very usefull for checkbox but when i am using RadioButtonList it is not working as on third page to first page. I am using this for online exam RadioButtonList is use for choice of answer i.e A ,B ,C and D.

private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in GridView1.Rows)
{


Label lbl = (Label)row.FindControl("Label1");
//index = Convert.ToInt32(lbl.Text);

// bool result = ((RadioButtonList)row.FindControl("RadioButtonList1")).SelectedValue;

RadioButtonList RD = (RadioButtonList)row.FindControl("RadioButtonList1");

// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];

for (int i = 0; i < RD.Items.Count; i++)
{
if (RD.Items[i].Selected == true)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
// string rem = RD.Items[i].ToString();

// string Qu = "Insert Into AnswerBank(UserName,QuestionID,SelectedAns) values ('Javed','" + ID + "','" + rem + "') ";
//dataAccess.SaveData(Qu);

if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
else
categoryIDList.Remove(index);
}

}

}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}


---

private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow
Title: It is nice but....   
Name: Naresh Gohil
Date: 2007-02-13 11:43:41 AM
Comment:
Hi,
I read this article and find the solution of my problem
But if i don't want to use primary key value than what's the solution.

... And I want to ask onr more question related to sorting

How to sort all the records in the gridview in paging
Title: Great Ariticle   
Name: Rajendran Thiagarajan
Date: 2007-02-12 12:03:00 PM
Comment:
Mohammad,

Thanks for your article. It's simple and great!!

-Rajendran Thiagarajan.
.Net Developer
Title: Session["CHECKED_ITEMS"] Error   
Name: Amol
Date: 2007-02-06 11:39:58 PM
Comment:
I'm getting this error.Can you help me with this
The name 'CHECKED_ITEMS' does not exist in the current context

Thanks
Title: Anonymous is right!   
Name: JonB
Date: 2006-12-15 5:58:09 PM
Comment:
Hey, thanks Azam.. before I read this article, I implemented this in a very similar fashion, except I add/remove to the Arraylist as I actually check the checkboxes (using postback for the actual checkbox. I had such a headache using the OnPageIndexChanged event, and after reading Anonymous's post, I moved the "repopulatecheck" method call into a DataBound even method and it worked. In fact, in my implementation, I don't even use the OnPageIndexChanged event at all! Still, thanks for creating this, it re-affirmed my implementation, and also lead to finding a solution. Thanks Azam, and Anonymous :)
Title: How did you fix it   
Name: Joe
Date: 2006-12-07 2:46:04 PM
Comment:
How did you fix the following problem? I am having the same issue.

Title: applying using datagrid, VB.NEt
Name: maya
Date: 12/1/2006 2:36:27 AM
Comment:
im using datagrid, VB.NEt, this should be work, but it cannot, it gave me error :-
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
maybe u could fix it :P .. :(
Title: Thanks Maya   
Name: AzamSharp
Date: 2006-12-01 10:34:42 PM
Comment:
Hi Maya,

I am glad that you found it useful.

:)
Enjoy the weekened!
Title: TQ   
Name: maya
Date: 2006-12-01 10:18:17 PM
Comment:
i've finally fixed it.. thanx u!! really.. this is an awesome article.. b4 this yur article popup sending values also save my day... so this is the 2nd time uu help me.. yeayyyyyyyyy
Title: applying using datagrid, VB.NEt   
Name: maya
Date: 2006-12-01 2:36:27 AM
Comment:
im using datagrid, VB.NEt, this should be work, but it cannot, it gave me error :-
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
maybe u could fix it :P .. :(

Private Sub RememberOldValues()
Dim categoryIDList As ArrayList = New ArrayList
Dim index As Integer = -1

For Each row As DataGridItem In dgList.Items
index = CType(dgList.DataKeys(row.ItemIndex).Value, Integer)

Dim result As Boolean = CType(row.FindControl("chkSelect"), CheckBox).Checked

If Not (Session.Item("CHECKED_ITEMS") Is Nothing) Then
categoryIDList = CType(Session("CHECKED_ITEMS"), ArrayList)
End If

If result Then
If Not categoryIDList.Contains(index) Then
categoryIDList.Add(index)
End If
Else
categoryIDList.Remove(index)
End If
Next

If Not (categoryIDList Is Nothing) AndAlso categoryIDList.Count > 0 Then
Session.Item("CHECKED_ITEMS") = categoryIDList
End If

End Sub

Private Sub RePopulateValues()
Dim categoryIDList As ArrayList = CType(Session.Item("CHECKED_ITEMS"), ArrayList)
If Not (categoryIDList Is Nothing) AndAlso categoryIDList.Count > 0 Then
For Each row As DataGridItem In dgList.Items
Dim index As Integer = CType(dgList.DataKeys(row.ItemIndex).Value, Integer)
If categoryIDList.Contains(index) Then
Dim myCheckBox As CheckBox = CType(row.FindControl("chkSelect"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub
Title: gridview and datagrid   
Name: maya
Date: 2006-12-01 1:08:26 AM
Comment:
hello azam,

i've tried applying this code to datagrid.. but the code behind gave syntax error on "rows is not a member of datagrid"... btw,what's the difference?
Title: RE: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: AzamSharp
Date: 2006-11-23 11:43:11 AM
Comment:
Download the sample and you will find out.
Title: RE: a little question!   
Name: AzamSharp
Date: 2006-11-23 11:41:44 AM
Comment:
Hi xiaofen_zh2003@163.com,

You need to check that the DataKeys contains the required datatype which is int.

You can try something like this:

int index = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

Hope it helps!
Title: Maintaining state of checkboxes   
Name: Abongile
Date: 2006-11-23 4:05:24 AM
Comment:
This is a great example, I've just used it and it worked the first time
Title: Repopulating checkboxes in GridView   
Name: Ali
Date: 2006-11-23 2:18:28 AM
Comment:
Reply to Liza:
problem is not with Azam's code,its just perfect.
what seems 2 me is you have set checkbox's autopostback property to true which causes da problem. As if Asp.net checkbox's autoppostback property is set to true, then when its contianer control's databind is called, checkbox's checkedchanged event is fired, which is i duno abnormal behavior or wat eva. how to fix it probably Azam bhai will tell us..;) pleez bro..!
Title: a little question!   
Name: xiaofen_zh2003@163.com
Date: 2006-11-22 11:45:24 PM
Comment:
Hello Mr. Azam,first thank you very much for your help about The GridView Control.but i have a question about using it. when the program run on
"index = (int)GridView1.DataKeys[row.RowIndex].Value", it happened with "System.InvalidCastException: convert is invalid" how to resolve it?
xiaofen_zh2003@163.com. thank you very much!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: KamalRaturi
Date: 2006-11-20 5:32:25 PM
Comment:
The Article is very helpful.
It is easy to understand and Implement.

Thanks!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Ahad
Date: 2006-11-20 7:11:03 AM
Comment:
Great Artical, very easy to follow:)
Title: Doesn't get the true value for checked CheckBox   
Name: kashif
Date: 2006-11-18 12:12:43 AM
Comment:
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;
im getting "false" value for even checked CheckBox, can't find anything wrong. Please help!

Note: There's no column for checkbox in the database, i want to use checkbox to select a value.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: ArabASP
Date: 2006-11-15 2:45:04 PM
Comment:
This line could cause me error : Session[CHECKED_ITEMS]
Can you tell us where to define this?
Title: Realy A very very good piece of work.   
Name: Soma
Date: 2006-11-10 4:19:35 AM
Comment:
It realy help me a lot. Thank u very much.........
Title: Creating The GridView Control   
Name: Jayakumar
Date: 2006-11-09 4:27:59 AM
Comment:
Thanks for the article. It reduces my time. :)
Title: Good piece of code   
Name: RED
Date: 2006-11-07 1:59:51 PM
Comment:
This is a good example on how to treat checkboxes inside the GridView Component. Thanx
Title: Thanks   
Name: AzamSharp
Date: 2006-11-07 11:29:31 AM
Comment:
I am glad that it helped you!
Title: Creating The GridView Control   
Name: Cuong Nguyen
Date: 2006-11-02 2:37:51 AM
Comment:
Thanks a lot
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: krishna som
Date: 2006-10-11 2:31:55 AM
Comment:
it helped me lot ... thanks
Title: CheckBox with GridView   
Name: Greivin Batz
Date: 2006-09-28 11:12:01 AM
Comment:
Thank you your help.... very helpful !!!
Title: Work with a textbox   
Name: scott
Date: 2006-08-30 11:42:01 AM
Comment:
Hey gridviewguy, can this example also work for remembering what was entered into a textbox? If so how?
Title: Re Repopulating checkboxes in GridView solution   
Name: Asif
Date: 2006-08-28 8:53:12 AM
Comment:
hi anonymus,

thanks for the solution but can you elaborate the code a bit for a clear understanding of every one
Title: Thanks   
Name: Azamsharp
Date: 2006-08-13 8:44:00 PM
Comment:
Thanks!
Title: Repopulating checkboxes in GridView solution   
Name: Anonymous
Date: 2006-08-13 8:30:45 PM
Comment:
This is a great solution! However, when I first tested I get the same problem as everyone where the checkboxes get reset after every page changed. In order to solve this problem, you need to add DataBound event to repopulate the checkboxes states. Below is the solution:

protected void GridView1_DataBound(Object sender, EventArgs e)
{
RePopulateValues();
}

protected void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
RememberOldValues();
}

Note: This is done in .Net 2.0 and the reason the author code doesn't work is because the databound event is called after pageindexchanging and pageindexchanged and therefore it clears the checkboxes states.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Thendral
Date: 2006-07-20 6:42:21 AM
Comment:
i used same code as in site but i erroe stating that ..
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


any ideas...
Title: PreRender?   
Name: Collin
Date: 2006-07-10 3:41:39 PM
Comment:
Since my checkbox is clearly getting checked in the RePopulate function, it be getting cleared somewhere later in the event model. I used Trace output to check the value of myCheckbox.Checked at various points in the page life cycle and found that it was being reset to false at PreRenderComplete. It is true during PreRender and then false during PreRenderComplete.

Why is this happening? Shouldn't the values of controls be fixed by the time we get to PreRenderComplete? The .NET Framework Class Library says this about PreRenderComplete:

"The PreRenderComplete event is raised when the pre-render stage of the page life cycle is complete. At this stage of the page life cycle, all controls are created, any pagination required is completed, and the page is ready to render to the output."

It mentions pagination but doesn't say exactly what occurs regarding this.

Now, if I move the RePopulate function to PreRenderComplete, the checkboxes get populated correctly and it seems to work. But this is a significant deviation from the author's solution. Does anybody know what's happening here?
Title: Same Problems   
Name: Collin
Date: 2006-07-09 9:56:02 PM
Comment:
I am having the same problems that others mentioned. I am adding the correct items to the Session object, but when I set 'myCheckBox.Checked = true' in RePopulateValues, nothing happens to the checkboxes. Pretty much exactly as Seth described.

Liza, I'm not sure if I understand your last comment. Are you saying the method of data binding is the problem? That is, manually binding the data instead of using a DataSourceId and GridView.DataBind? I am also using DataSourceId. There's no reason I can see why that shouldn't work. I can try to manually bind the data as Mohammad does and see what happens.

If anybody has any more insight into this problem it would be much appreciated (especially the author!)

Thanks.
Title: Reply To Seth   
Name: Liza
Date: 2006-07-06 1:15:44 PM
Comment:
Hi Seth,
I wanted you to know that I used the exact example as Mohammed gave and the checkboxes are now maintained. I was using a defined DataSourceID so that I could cut down the database based on a parameter. My previous not-working example contained a where clause in the datasource ID. I had to select ALL the columns in the database, but at least I got the example to work. Now I'll work on incorporating my WHERE clause and I'll let you know how it goes.
Title: Problems   
Name: Seth
Date: 2006-06-28 9:16:01 PM
Comment:
What is happening to Liza is also happening to me; I have figured out that the new page's checkboxes are not being displayed and instead the current page is being checked during repopulate... not sure why, but perhaps theres a different place to call repopulate than in the PageIndexChanging method, as for us it does not actually update the new page when we change the page index, but does so after that method call completes.
Title: Maintaining state of checkboxes   
Name: Moupiya
Date: 2006-06-15 1:27:22 AM
Comment:
Hi!!!
I used your code.The values are coming in the function but finally when the page loads the check boxes are not checked.
I am using GridView DataBind() instead of BindData function.Moreover i have used HtmlCheckBoxes.
Please help out..
moupiyadas@yahoo.com
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Swap
Date: 2006-05-04 3:05:04 AM
Comment:
Thanks a lot.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: alyque
Date: 2006-03-23 8:36:52 PM
Comment:
Very helpful... Thanks!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Lola
Date: 2006-03-22 9:15:29 AM
Comment:
Good job on this article
Title: Repopulating checkboxes in GridView   
Name: Liza Pulsifer
Date: 2006-03-07 9:32:18 AM
Comment:
Hello Mr. Azam,
I used your code snippets in an effort to repopulate the checkboxes in a GridView. The code appears to check the appropriate boxes. I set a breakpoint at the and verified variables at the line "myCheckBox.Checked = true;" However, when the page finally appears, the checkboxes are empty. I use "grdGridView.DataBind();" instead of your "BindData()"
subroutine. Any ideas why the checkboxes are not appearing?
Title: Creating The GridView Control   
Name: bjack8468@satx.rr.com
Date: 2006-03-06 11:48:40 AM
Comment:
I finally got this to work so you can ignore the error message that I sent yesterday. I have one other question, do you know how to write this in VB for ASP; I would be most greatful. Good job on this article.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-10 3:25:07 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search