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

Three State checkboxes and events

2 Answers 122 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michal
Top achievements
Rank 1
Michal asked on 08 Sep 2011, 06:42 PM
Hello!
I am working on an application that uses grid view with checkboxes. When checkbox value is changed, it calls ValueChanged event with radGridView.EndEdit() and then calls CellValueChanged to read new data.

I want to expand checkboxes functionality with Three State mode however I want user only to be able to switch between normal On and Off states, while the Indeterminate state can only be assigned from the code. I want to make it so user can either go   On -> Off,   On  -> Off  or Indeterminate -> Off, and not On -> Indeterminate . 

The thing is, I can't find a right event, that should be called when checkbox value is changed and change it, so user cannot reach null state by clicking it. Normally, in Three State mode, when user clicks on checkbox in On state, Indeterminate state is called. I want to switch this state to Off  before reading the data.

I hope I've explained my problem clearly enough. Thank you for reading my post :)


2 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 09 Sep 2011, 05:53 AM
Hello Michal,

Just register for the DataBindingComplete event and set the checkbox column to the three state mode, like so:
void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
    var checkBoxColumn = radGridView1.Columns[2] as GridViewCheckBoxColumn;
    if (checkBoxColumn != null)
    {
        checkBoxColumn.ThreeState = true;
    }
}

Or if you are creating the columns manually just set that property on the column directly.

Hope this helps, if you have any other questions please let me know,

Best Regards,
Emanuel Varga

WinForms MVP
0
Svett
Telerik team
answered on 13 Sep 2011, 12:19 PM
Hello Michal,

You can use the ThreeState mode of the GridViewCheckBoxColumn as Emanuel mentioned. In addition to his suggestion, you can handle the ValueChanged event to achieve the desired behavior:
private bool isValueChanging = false;
 
private void radGridView1_ValueChanged(object sender, EventArgs e)
{
    if (isValueChanging)
    {
        return;
    }
 
    RadCheckBoxEditor checkBoxEditor = sender as RadCheckBoxEditor;
    ToggleState state = (ToggleState)checkBoxEditor.Value;
    if (checkBoxEditor != null && state == ToggleState.Indeterminate)
    {
        isValueChanging = true;
        checkBoxEditor.Value = ToggleState.Off;
        isValueChanging = false;
    }
}
 
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    this.radGridView1.DataSource = Data.GetDummyEmployees();
    GridViewCheckBoxColumn checkBoxColumn = this.radGridView1.Columns["SelfEmployeed"] as GridViewCheckBoxColumn;
    checkBoxColumn.ThreeState = true;
}

I hope this helps.
 
Regards,
Svett
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Michal
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Svett
Telerik team
Share this question
or