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

How to get selected checkboxes in RadGridView

2 Answers 743 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ghulam Haider
Top achievements
Rank 1
Ghulam Haider asked on 15 May 2010, 07:33 AM
Hi
I am working on RadGridView in winforms I have added my columns on design view and my last column is a checkbox and adding my datarows in grid by using given code

for (int i = 0; i <= dtPOs.Rows.Count - 1; i++)
                {
  dgPO1.Rows.Add(dtPOs.Rows[i]["CLM0"].ToString(), dtPOs.Rows[i]["CLM1"].ToString(), DateTime.Parse(dtPOs.Rows[i]["CLM1"].ToString()).ToString("dd-MMM-yyyy"));
                }

Now I want to get all rows in which checkbox is checked. and I am using the following code but fail

 for (int i = 0; i <= dgPO1.Rows.Count - 1; i++)
                {
                     bool isChecked = (Convert.ToBoolean(dgPO1.Rows[i].Cells[3].Value));
                   if(isChecked )
                    {
                          ...... my Logic
                     }
                }
But it is not working. Please tell me how I can get the selected checkbox value from RadGridView CheckBox colum.


2 Answers, 1 is accepted

Sort by
1
Svett
Telerik team
answered on 19 May 2010, 03:34 PM
Hi Ghulam,

If you want to get the value of the current active editor, you need to access ActiveEditor property of the grid:
RadCheckBoxEditor chEditor = this.radGridView.ActiveEditor as RadCheckBoxEditor; ;
if (chEditor != null)
{
    bool isChecked = Convert.ToBoolean(chEditor.Value);
}

Notice that the active editor can be null if you are not in edit mode. Also the value in editor can be different than the cell value if it is not committed yet.

If you want to get the value of a concrete cell, you need to use the Value property of the cell. You can use the following code snippet:
bool isChecked = Convert.ToBoolean(this.radGridView.Rows[0].Cells["BooleanColumn"].Value);

If you need further assistance, feel free to contact us back.

All the best,
Svett
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Suhaib
Top achievements
Rank 1
Iron
commented on 24 Apr 2024, 09:47 AM

How would you go about getting the actual checkbox element from all the RadGridView rows and enabling/disabling them?

 

This is what I'm doing at the moment:

foreach (var myGridRow in myGridView.Rows)
{
    curveGridRow.Cells["Visible"].ColumnInfo.ReadOnly = false;
}

How is the above code different from:

curveGridRow.Cells["Visible"].ReadOnly = false;

Lastly, the ActiveEditor check box can be enabled/disbaled like so:

RadCheckBoxEditor chkBoxVisible = myGridView.ActiveEditor as RadCheckBoxEditor;
chkBoxVisible.EditorElement.Enabled = false;

is there a way to similarly enable/disable the checkbox element in each grid row?

Thanks 

0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Apr 2024, 09:59 AM

Hello, Suhaib,

The first code snippet that you provided setting Cells["Visible"].ColumnInfo.ReadOnly will disable the readonly state on the whole column. Setting Cells["Visible"].ReadOnly will set the property to each cell from that column which is almost the same. 

Note, GridViewCheckBoxColumn offers ReadOnly property out of the box. In case you want to set it on the whole column, it is not necessary to iterate the Rows collection, you can just set the property to your GridViewCheckBoxColumn.

If you want to enable/disable the checkbox element for each cell in grid, you can subscribe to CellFormatting event:

 private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
 {
     GridCheckBoxCellElement checkBoxCell = e.CellElement as GridCheckBoxCellElement;
     if (checkBoxCell != null)
     {
            ( checkBoxCell.Editor as RadCheckBoxEditor).EditorElement .Enabled = false;
     }
 }

I hope this helps. If you have any further questions, please let me know.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Suhaib
Top achievements
Rank 1
Iron
commented on 26 Apr 2024, 01:32 PM

What I'm trying to achieve is that I have a RadGridView with rows that each has a checkbox, and I'm trying to set a limit on the maximum number of checkboxes that can be checked. When the maximum number of checkbox is reached, i.e. when a checkbox is checked and the number reaches the maximum, I'd like to disable all the other unchecked checkboxes 

how would I do this in MyGridView_ValueChanging?

Thanks Nadya!

Dinko | Tech Support Engineer
Telerik team
commented on 01 May 2024, 09:09 AM

I see what you are trying to achieve here. In the ValueChanging event, you can check the current type of the change value. If it is a boolean, you can keep its count. For example, if it reaches 3, you can set the ReadOnly property to the column to true. This will trigger the CellFormatting event. In the event handler, you can disable the editor elements inside that column.

private void RadGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    GridCheckBoxCellElement checkBoxCell = e.CellElement as GridCheckBoxCellElement;
    if (checkBoxCell != null && count == 3)
    {
        (checkBoxCell.Editor as RadCheckBoxEditor).EditorElement.Enabled = false;
    }
}

int count = 0;
private void RadGridView1_ValueChanging(object sender, Telerik.WinControls.UI.ValueChangingEventArgs e)
{
    if (e.NewValue.GetType() == typeof(bool) )
    {
        count++;
        if (count == 3)
        {
            this.radGridView1.Columns.Last().ReadOnly = true;                    
        }
    }
}

You can also check my test project and extend the code to cover your requirements.

Tags
GridView
Asked by
Ghulam Haider
Top achievements
Rank 1
Answers by
Svett
Telerik team
Nadya | Tech Support Engineer
Telerik team
Share this question
or