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

GridView ValueChanged behaving strange

2 Answers 470 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mohammed
Top achievements
Rank 1
Mohammed asked on 17 Jul 2016, 06:14 PM

I have a GridView with some bound data. I have an unbound checkbox on the first column... This checkbox simply acts as a marker for further processing... eg on all checked rows, data is saved when a button is pressed...

On change of each checkbox I need to run some validation... which I won't go into here...So I use the event MyGridView_ValueChanged(object sender, EventArgs e).

From this event I use the following code to go through each row and find all the checked checkboxes...

            foreach(GridViewRowInfo gvri in MyGridView.Rows)
            {
                if (Convert.ToBoolean(gvri.Cells["chkSelected"].Value) == true)
                {
                       // Happy Days
                 }
            
}

Even though checking this checkbox fires the ValueChanged event, when this code is run, there are no values that return true for the checkboxes.  If I select another checkbox, the first one now appears as true, but the one I just selected remains false... Why does this not show the changed value in the grid?  Also can you advise if this is possible using any other events / properties?

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 18 Jul 2016, 07:43 AM
Hello Mohammed,

Thank you for writing.

The observed behavior is expected since the cell remains in edit mode. In order to achieve the desired behavior, you can set the EditMode property of the checkbox column to OnValueChange. This will process the new value immediately as it is entered: EditMode.

Additionally, you may consider handling the CellValueChanged event which is raised before the ValueChanged event of the grid: 
public Form1()
{
    InitializeComponent();
   
    this.radGridView1.DataSource = this.GetSampleData();
    GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn();
    checkBoxColumn.EditMode = EditMode.OnValueChange;
    checkBoxColumn.Name = "chkSelected";
    radGridView1.MasterTemplate.Columns.Add(checkBoxColumn);
 
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    this.radGridView1.CellValueChanged += radGridView1_CellValueChanged;
}
 
private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    foreach (GridViewRowInfo gvri in this.radGridView1.Rows)
    {
        if (Convert.ToBoolean(gvri.Cells["chkSelected"].Value) == true)
        {
        }
    }
}
 
private DataTable GetSampleData()
{
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add("Id", typeof(int));
    dataTable.Columns.Add("Name", typeof(string));
    dataTable.Columns.Add("Date", typeof(DateTime));
 
    for (int i = 0; i < 100; i++)
    {
        dataTable.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i));
    }
 
    return dataTable;
}

I hope this helps. Please let me know if you need further assistance.

Regards,
Hristo Merdjanov
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
0
Mohammed
Top achievements
Rank 1
answered on 19 Jul 2016, 10:06 AM

Perfect!!! Thank you for your response Hristo.  The edit mode was the key, but I also made use of your CellValueChanged suggestion. Thanks Again!

Tags
GridView
Asked by
Mohammed
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Mohammed
Top achievements
Rank 1
Share this question
or