vendredi 17 juillet 2015

Looping through Grid view rows gives ArgumentOutOfRangeException while accessing GridView.DataKeys property

I have a gridview and I am having two checkboxes in my gridview. I am performing sort and retaining the values of checkboxes either checked or unchecked, while sorting. This is how I am doing:

    protected Dictionary<int, PersistCheckboxState> checkBoxState; //Dictionary object I created


    protected void grdResults_Sorting(object sender, GridViewSortEventArgs e)
    {
        this.checkBoxState = new Dictionary<int, PersistCheckboxState>();

        foreach (GridViewRow row in grdResults.Rows)
        {
            // Get references to the CheckBoxes
            CheckBox CB1 = (CheckBox)row.FindControl("chk1");
            CheckBox CB2 = (CheckBox)row.FindControl("chk2");

            // The default state is both CheckBoxes unchecked, so only save state if one is checked
            if (CB1.Checked || CB2.Checked)
            {
                // Get DataKey for reference.  This will be used to retrieve the state later.
                int key = Int32.Parse(grdResults.DataKeys[row.RowIndex].Value.ToString());

                // Save CheckBox state.
                PersistCheckboxState state = new PersistCheckboxState(CB1.Checked, CB2.Checked);
                this.checkBoxState.Add(key, state);
            }
        }

        if (SortState == e.SortExpression)
        {
            if (SortDir == "DESC") SortDir = "ASC";
            else SortDir = "DESC";
        }
        else
        {
            SortDir = "ASC";
        }
        SortState = e.SortExpression;

        Refresh_DataGrid();
    }

    protected void grdResults_DataBound(object sender, EventArgs e)
    {
        if (checkBoxState != null)
        {
            // Loop through each row in the GridView...
            foreach (GridViewRow row in grdResults.Rows)
            {
                // Get DataKey
                int key = Int32.Parse(grdResults.DataKeys[row.RowIndex].Value.ToString()); //This is where I am having the issue, where the exception is thrown.
                PersistCheckboxState state;

                // Check if a state exists for this key.
                if (checkBoxState.TryGetValue(key, out state))
                {
                    // Get references fo CheckBoxes
                    CheckBox CB1 = (CheckBox)row.FindControl("chk1");
                    CheckBox CB2 = (CheckBox)row.FindControl("chk2");

                    // Restore state
                    CB1.Checked = state.chkbox1;
                    CB2.Checked = state.chkbox2;
                }
            }
        }
    }

This is my class, I am using to retain checkbox state

public class PersistCheckboxState
{
    public PersistCheckboxState(bool ischkdCB1, bool ischkdCB2)
    {
        this._ischkdCB1 = ischkdCB1;
        this._ischkdCB2 = ischkdCB2;
    }


    private bool _ischkdCB1 ;
    public bool ChkBox1
    {
        get { return _ischkdCB1 ; }
        set { _ischkdCB1 = value; }
    }


    private bool _ischkdCB2 ;
    public bool ChkBox2
    {
        get { return _ischkdCB2 ; }
        set { _ischkdCB2 = value; }
    }
}

So, I debugged through the code and found that, when I loop through my gridview rows using: foreach (GridViewRow row in grdResults.Rows)

I get Count = 14 for grdResults.Rows

When I expand the result set there, I see:

{0}  RowIndex = 0   DataItemIndex = 0
{1}  RowIndex = 1   DataItemIndex = 1  
{2}  RowIndex = 2   DataItemIndex = 2  
.  
.   
.  
{13}  RowIndex = 13   DataItemIndex = 13

So, when it loops, the first index will be {0} which has RowIndex = 0 DataItemIndex = 0 and when it reaches a point where I am getting the DataKey:

int key = Int32.Parse(grdResults.DataKeys[row.RowIndex].Value.ToString());

It has RowIndex as 0 so I am getting the exception here. I am not sure on how to get this problem resolved.
Any help will be appreciated.

Thanks.




Aucun commentaire:

Enregistrer un commentaire