Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

manage checkbox status in datagrid in C#

0.00/5 (No votes)
12 Jan 2006 1  
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