This is a migrated thread and some comments may be shown as answers.

Error in Events while select checkbox column

1 Answer 106 Views
GridView
This is a migrated thread and some comments may be shown as answers.
muralitharan
Top achievements
Rank 1
muralitharan asked on 10 Feb 2009, 01:18 PM
Hi,
I am using check box column to select row.
In fact when the check box is selected, I am storing the DataBondItem of checked row in an Arraylist.

The constraint here is, at a time only one check box has to be checked.
When the next row is checked. The privious check box should be unchecked.

The above is the total functionality.

I am using, BeginEdit and ActiveEditorValuechanged event to perform the above functionality.

The problem here is I am getting 'Object Reference Error' once in a while.

Can anybody suggest better way to obtain the above functionality ?

Current Code for reference :

 void radGrid_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            currentRowIndex = e.RowIndex;
            CurrentRowDataItem = this.radGrid.Rows[e.RowIndex].DataBoundItem;

            try
            {
                CurrentRowDataItem = this.radGrid.Rows[e.RowIndex].DataBoundItem;
                if (radGrid.Columns[e.ColumnIndex] is GridViewCheckBoxColumn)
                {
                    //When the editor is clicked properly
                    if (radGrid.ActiveEditor != null && radGrid.ActiveEditor.Value != null)
                    {
                        //create handler for active Editor
                        radGrid.ActiveEditor.ValueChanged += new EventHandler(ActiveEditor_ValueChanged);
                    }
                }
            }
            catch { }
        }

        void ActiveEditor_ValueChanged(object sender, EventArgs e)
        {
            //Only when the value is not null
            if (radGrid.ActiveEditor != null && radGrid.ActiveEditor.Value != null)
            {
                //remove the handler, because next time when the editor is clicked new handler wil be created
                radGrid.ActiveEditor.ValueChanged -= new EventHandler(ActiveEditor_ValueChanged);
                try
                {
                    //Here check the any row's check box selected in the Grid or not.
                    if (this.selectedRows != null && radGrid.ActiveEditor != null)
                    {
                        //current data item
                        object item = radGrid.CurrentRow.DataBoundItem;

                        if (radGrid.ActiveEditor.Value.ToString().ToLower() == "true")
                        {
                            this.selectedRows.Clear();
                            this.selectedRows.Add(item);

                            if (PreviousRowDataItem == null || previousCheck == -1)
                            {
                                //previousCheck = currentRowIndex;
                                PreviousRowDataItem = this.radGrid.CurrentRow.DataBoundItem;
                            }
                            
                            //set current index of previously selected row. It has to be checked and set because while sorting index may change
                            previousCheck = GetActualCheck(PreviousRowDataItem);

                            //Uncheck the previously selected
                            if (previousCheck != -1)
                            {
                                //set false if its true
                                if (radGrid.Rows[previousCheck].Cells[0].Value.ToString().ToLower() == "true")
                                {
                                    radGrid.Rows[previousCheck].Cells[0].Value = Boolean.FalseString;
                                }
                            }
                            //change the previous rowdata to current dataitem
                            if (currentRowIndex != -1)
                            {
                                PreviousRowDataItem = this.radGrid.CurrentRow.DataBoundItem;
                            }

                            if (OnSelectionChange != null)
                            {
                                OnSelectionChange();
                            }
                        }
                        //if uncheck clear the selected rows
                        else
                        {
                            this.selectedRows.Clear();
                        }

                        //if the Edit is finished, have to call this Default function otherwise the edit should not be commited
                        bool b = radGrid.EndEdit();
                    }
                }
                catch (Exception ex)
                {
                    Nexus.UI.BaseMdiParent.RaiseException(this, ex);
                }
            }
        }

1 Answer, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 11 Feb 2009, 04:53 PM
Hi muralitharan,

Please find the answer to your question in you first forum post.

Best wishes,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
muralitharan
Top achievements
Rank 1
Answers by
Nick
Telerik team
Share this question
or