Click here to Skip to main content
Licence 
First Posted 12 Jan 2006
Views 28,988
Downloads 91
Bookmarked 17 times

manage checkbox status in datagrid in C#

By | 12 Jan 2006 | Article
keep checkbox status in datagrid

Introduction

The article demostrated using array and session to keep the status of checkbox in datagrid.

In ASP.NET, Datagrid could automatically seperate page. Yet for each item on every page, how could we keep the status of checkbox if we have checkbox in the datagrid? We could use javascrip, we could also use a control to keep the checked checkbox index. The fowwoing example is using session to keep the status of checkbox.

The first step is to define array to save the status of checkbox and a variable to store the counter:

protected int iCounter = -1;
//Then put the status in the array:
{
    iCounter2= (
    {
        iIndex++;
        CheckBox cb=(CheckBox)griditem.Cells[0].Controls[0];
        if(cb.Checked) int check=0;for(int 
            i=0;i<arrayRiderIDs.Length;i++)
            if(iId==arrayRiderIDs[i])if(check==0)else
        {
            {
                {
                    arrayRiderIDs[i]=0;
                }
            }
        }
    }
    for(int i=0;i<arrayRiderIDs.Length;i++)if(iId==arrayRiderIDs[i])
       // after status is saved in array, save the array and counter in 
       //session:<BR>
    Session["arrPersonID"]= iCounter2;
    Session["arrPerson"]= arrayRiderIDs;
    // after you return back to the page, you could use itemcreated to get  
    // status:
    private
    {
        {
            CheckBox cb=
                TableCell cell=e.Item.Cells[0];
            {
                {
                    check=1;
                }
            }
            {
                cb.Checked=
            }
            iCounter++;
            arrayIDs[iCounter]=ID;
            cell.Controls.Add(cb);
        }
    }

All of these are server-side.

void dgRiders_ItemCreated(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == 
    ListItemType.AlternatingItem)
    new CheckBox();
    int ID=(int)dgRiders.DataKeys[e.Item.ItemIndex];
    int check=0;for(int i=0;i<arrayRiderIDs.Length;i++)
        if(ID==arrayRiderIDs[i])if(check==1)true; 
        {
            {
                {
                   check=1;
                }
            }
        }
iCounter2++;
arrayRiderIDs[iCounter2]=iId;

private void dgRiders_ItemCommand(object source, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    if(Session["arrPersonID"]!= null)int) 
        Session["arrPersonID"];
        int iIndex=-1;
        foreach(DataGridItem griditem in dgRiders.Items)
            int iId=arrayIDs[iIndex];
            /* Check the checkbox status, if checked and the checkbox ident is 
            /* not saved in arrayRiderIDs, save to arrayRiderIDs */ 
        protected int[] arrayRiderIDs= new int[20];

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chenggui

Web Developer
www.emiccorp.com
United States United States

Member

Chenggui Chu is Lead Sharepoint Developer at IQ solutions.Right now, he is focused on Sharepoint Development. You could reach him at chu7319@yahoo.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 12 Jan 2006
Article Copyright 2006 by Chenggui
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid