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

CheckBox Column Set based on Value in Text Field

5 Answers 167 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 2
Veteran
Mark asked on 29 Jan 2018, 06:52 PM
I want to have a check box column, that if there is test/value in column1, I want to check box to be checked.  If there isn't any value, I don't want it to be checked. What's easiest way to implement this?

5 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 30 Jan 2018, 08:42 AM
Hi Mark,

Once the grid is loaded you can iterate all rows and set the value. Then you will need to update it only when the other value is changed. Here is the code:
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    foreach (GridViewRowInfo row in radGridView1.Rows)
    {
        if (row.Cells["Name"].Value == null || row.Cells["Name"].Value == DBNull.Value)
        {
            row.Cells["CheckBoxCol"].Value = false;
        }
        else
        {
            row.Cells["CheckBoxCol"].Value = true;
        }
    }
    radGridView1.CellValueChanged += RadGridView1_CellValueChanged;
}
  
private void RadGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "Name")
    {
        if (e.Value == null || e.Value == DBNull.Value)
        {
            e.Row.Cells["CheckBoxCol"].Value = false;
        }
        else
        {
            e.Row.Cells["CheckBoxCol"].Value = true;
        }
    }
  
}

I hope this will be useful. Let me know if you have additional questions.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 2
Veteran
answered on 30 Jan 2018, 02:11 PM

This is ok, but I am data binding the grid.  I am then manually adding a checkboxgridcolumn after the binding and I want that check box to be checked if data in a field is not null and not empty.

0
Dimitar
Telerik team
answered on 31 Jan 2018, 08:47 AM
Hello Mark,

You can change the "if' statement like this:
if (row.Cells["Name"].Value != null && row.Cells["Name"].Value.ToString() != String.Empty )
{
    row.Cells["CheckBoxCol"].Value = false;
}
else
{
    row.Cells["CheckBoxCol"].Value = true;
}

Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 2
Veteran
answered on 31 Jan 2018, 01:50 PM
So, i have to manually update each row's checkbox column when I load the data?   I was hoping for something a little more automatic. 
0
Dimitar
Telerik team
answered on 01 Feb 2018, 08:24 AM
Hi Mark,

Another approach you can use is to create a custom function that checks for this and use it with expression column. Detailed information is available in the following articles:
Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Mark
Top achievements
Rank 2
Veteran
Answers by
Dimitar
Telerik team
Mark
Top achievements
Rank 2
Veteran
Share this question
or