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

Using CheckBoxColumn

6 Answers 393 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, 12:17 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);
                }
            }
        }






6 Answers, 1 is accepted

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

Thank you for your question. Please refer to the following code:

private void Form1_Load(object sender, EventArgs e) 
        { 
            this.radGridView1.MasterGridViewTemplate.Columns["column1"].ReadOnly = true
        } 
 
        GridViewRowInfo prevRow = null
 
        private void radGridView1_SelectionChanged(object sender, EventArgs e) 
        { 
            if (prevRow != null)  
                prevRow.Cells["Column1"].Value = false
            prevRow = this.radGridView1.SelectedRows[0]; 
            this.radGridView1.SelectedRows[0].Cells["Column1"].Value = true
 
        } 

Does the sample code cover your needs? Do not hesitate to write me back if you have further questions.

All the best,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
muralitharan
Top achievements
Rank 1
answered on 12 Feb 2009, 10:58 AM
Hi

Thanks for your reply
I will try this
0
muralitharan
Top achievements
Rank 1
answered on 12 Feb 2009, 05:02 PM
Hi,
I Implemented your code.
I am having a problem in this way.  At first time I am getting radgrid1.selectedrows.count = count 1. So I am able to proceed with the row.
But at the second time, when I select some other row...  the selection changed event is triggered But,
radgrid1.selectedRows.count = 0. 
So I am unable to get newly selected row.
Error -
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

 void radGrid_SelectionChanged(object sender, EventArgs e)
        {
            if (CheckSingle)
            {
                if (prevRow != null)
                    prevRow.Cells[0].Value = false;
                prevRow = this.radGrid.SelectedRows[0];
                object item = this.radGrid.SelectedRows[0].DataBoundItem;
                this.selectedRows.Clear();
                this.selectedRows.Add(item);
                this.radGrid.SelectedRows[0].Cells[0].Value = true;
            }
        }

The Bold line throws error as this.radGrid.SelectedRows.count = 0 even though
this.radGrid.SelectionMode = GridViewSelectionMode.FullRowSelect

0
Accepted
Nick
Telerik team
answered on 13 Feb 2009, 04:11 PM
Hello muralitharan,

Thank you for contacting me back. I am not sure I can understand you completely. Could you please clarify what is the purpose of those three lines:

object item = this.radGrid.SelectedRows[0].DataBoundItem; 
this.selectedRows.Clear(); 
this.selectedRows.Add(item); 

Please give me your scenario in detail and I will think of the right solution.

All the best,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
muralitharan
Top achievements
Rank 1
answered on 25 Aug 2009, 05:40 AM

Hi,
     While filtrering column values by radgridview, we are getting the following error.

    Value cannot be null.

    Parametername: Key

Kindly let me know your thoughts.

 

    

0
Vassil Petev
Telerik team
answered on 25 Aug 2009, 08:37 AM
Hello muralitharan,

Is your question related to the question you asked here previously? If so, how? If it is not related, please open a new support thread and give us more details on the code you are using, so that we can assist you properly.


Kind regards,
Vassil
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
muralitharan
Top achievements
Rank 1
Answers by
Nick
Telerik team
muralitharan
Top achievements
Rank 1
Vassil Petev
Telerik team
Share this question
or