GridView ValueChanged behaving strange

3 posts, 1 answers
  1. Mohammed
    Mohammed avatar
    19 posts
    Member since:
    Jan 2016

    Posted 17 Jul Link to this post

    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. Answer
    Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    710 posts

    Posted 18 Jul Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mohammed
    Mohammed avatar
    19 posts
    Member since:
    Jan 2016

    Posted 19 Jul in reply to Hristo Merdjanov Link to this post

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

Back to Top