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

Set CheckBox via sourcecode results in look

2 Answers 16 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gabriele
Top achievements
Rank 1
Gabriele asked on 26 Feb 2014, 11:39 AM
Hi all,

either this Wednesday is a Monday or I am just a bit too stupid to get it right today.

I have a GridView which has a CheckBoxColum. Now I want to set the checkbox to checked in one row when the GridView is bound based on a value that is sorted in another field in the datasource. The value is only know on Startup and is determinded by another Function.

However, when I enable the code the application seems to go into an endless-loop - it keeps repeating the "CellFormatting"-Event on and on and never starts up.
The datasource of the grid is a List of objects that is collected and then bound on Startup and in this test-Scenario contains 3 elements. As soon as I delete the code shown below the application starts up without any Problems.

Any help would be appreciated. Thank you.

This is the code:

        private void gvPersonalKeys_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo is GridViewCheckBoxColumn)
            {
                GridCheckBoxCellElement box = (GridCheckBoxCellElement)e.CellElement;
 
                string value = e.CellElement.RowInfo.Cells["Serialnumber"].Value.ToString();
            
                if (value == "738F")
                    box.Value = true;
                else box.Value = false;
            }
        }

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Mar 2014, 09:41 AM
Hello Gabriele,

Thank you for contacting Telerik Support.

Setting the value of a GridCheckBoxCellElement in the CellFormatting event will fire the event again. That is why the recommended approach to synchronize the check box with a specific field in the DataBoundItem is to set the value only when it is necessary. Here is a sample code snippet:
public Form1()
{
    InitializeComponent();
 
    List<Item> items = new List<Item>();
    for (int i = 0; i < 10; i++)
    {
        string serial = i % 2 == 0 ? "738F" : "200F";
        items.Add(new Item(i, serial));
    }
 
    this.radGridView1.DataSource = items;
    GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn();
     
    radGridView1.MasterTemplate.Columns.Add(checkBoxColumn);
}
 
public class Item
{
    public int ID { get; set; }
 
    public string SerialNumber { get; set; }
     
    public Item(int iD, string serialnumber)
    {
        this.ID = iD;
        this.SerialNumber = serialnumber;
    }
}
 
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.RowIndex > -1 && e.CellElement.ColumnInfo is GridViewCheckBoxColumn)
    {
        GridCheckBoxCellElement box = (GridCheckBoxCellElement)e.CellElement;
 
        string value = e.CellElement.RowInfo.Cells["SerialNumber"].Value.ToString();
 
        if (box.Tag == null)
        {
            box.Tag = "Updated";
            if (value == "738F")
                box.Value = true;
            else
                box.Value = false;
        }
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
0
Gabriele
Top achievements
Rank 1
answered on 03 Mar 2014, 11:47 AM
Hey,

thank you. That did the trick!
Tags
GridView
Asked by
Gabriele
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Gabriele
Top achievements
Rank 1
Share this question
or