Selection changed event raised on adding a row to a DataGridView - c# express?

Hi,

I have a form with a Datagridview control which gets filled programatically.
It also has an associated 'selection changed' event for when the user gets to interact.

In beta 1 all was OK but in beta 2 the selection changed event is raised everytime the program adds a row which is definitely not what I want.

Anyone know if this behaviour is by design

As a workaround I guess I could disable the selection changed events and then enable them again

Which leads on to the Newbie question how do I disable the event which has been set up in the designer section using the properties pane

Many thanks for reading
Cheers


Answer this question

Selection changed event raised on adding a row to a DataGridView - c# express?

  • Qi Sheng

    And a little more information.

    I installed c# express on another computer - one that never had beta 1 installed and ran the above code - it fires the selection event when the first row is added and the first row is selected automatically as it is added.

    So different but still not as I think beta 1 used to be.

    And finally I made the same thing on a PC still running beta 1.
    And guess what, it does not fire the event and the first row added is not selected!

    I've been through and checked all the properties and as far as I can tell they are all the same (some names have been changed between beta 1 and 2)

    So where are we

    I have a repeatable difference between beta 1 and beta 2.
    I have a difference in behaviour between two beta 2 installs on different computers.

    Edit - the difference between the two beta 2 installs was down to the enable adding property - with it set to false the datagridview is created empty, withit set to true it is created with a blank entry which fires the selection changed event.

    So now I'm only left with a difference in behaviour between beta 1 and 2.

    Any advice is more than welcome

    Cheers
    Steve

  • genocide77

    I just tried doing this but couldn't repro the issue -- I have a button on my form and I call dataGridView1.Add() in the button click. I also have an event handler to SelectionChanged and write to the debug window. When I click the button I don't get any SelectionChanged event.

    Can you provide more details - maybe a code sample

    thanks,
    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"


  • Hossam El-Deen

    A bit more new information - I liked the idea of just testing this with a simple app so I put a datagrid view and a button on a form and added the following code

    namespace Datgridview_test

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    object [] newrow = {"fred","a row"};

    dataGridView1.Rows.Add(newrow);

    }

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)

    {

    MessageBox.Show("Selection changed Event fired");

    }

    }

    }

    You are quite right pressing the button doesn't fire the selection changed event but it fires when the code is run.  Single stepping through the event is fired afer the initialixeComponent routine is done and Program executes

    Application.Run(new Form1());

    I'm beginning to think this coud have something to do with my installation.  I had problems going from beta 1 to 2 but they went away after I ran the beta 1 to beta 2 cleanup tool.

    Any other ideas greatfully received.
    Steve


  • Eric Murphy

    Hi Mark, thanks for trying to reroduce.

    Some more details My DataGrid is on a tab page
    The idea is to fill it with information from a remote system.  I read the information in and then when the comms are all over and done with parse the information and add it into the grid.

    I have a selection changed event assigned to the data gridview so that I can update the rest of the form with relevant information when the user highlights any item in the data grid.

    Some code snippets

    This has my workaround on it - the loadingdatagrid flag
    The comms data is in the array xmlcomms, rows is the datagridview rows collection
    The clear selection bit is part of the workaround to - else the first item is selected.

    loadingdatagrid = true;
                    foreach (string direntry in xmlcomms)
                    {

                        object[] clipparams = parseQdirentry(direntry);
                        if (clipparams[0].ToString() != "error")
                        {
                            clipparams[0] = false;
                            rows.Add(clipparams);      //Debug jumped to selection changed event here
                        }
                    }
                    la_head.Text = "Directory transferred, " + rows.Count.ToString() + " clips";
                    Log.WriteLine("Directory transferred, " + rows.Count.ToString() + " clips");
                    checkBox2.Visible = true;
                    Qdir.ClearSelection();
                    loadingdatagrid = false;

    This is the selection changed event (with workaround)

    private void Qdir_SelectionChanged(object sender, EventArgs e)
            {
                //workaround added in beta2 to prevent this event getting
                //in the way of getting a directory from Quantel
                if (loadingdatagrid) return;

                //do an update to show the selection instantly
                Qdir.Update();
                if (Qdir.SelectedRows.Count != 0)
                {
                    if (Qdir.SelectedRows[0] != currentrow)
                    {
                        //OK we have a valid selection so enable the labels
                        currentrow = Qdir.SelectedRows[0];
    snip.

    New information is that it is only the first row that ends up selected.

    I single stepped through everything and suddenly found myself in the selection changed routine which is the only event I have firing from the datagridview

    Cheers
    Steve

  • Selection changed event raised on adding a row to a DataGridView - c# express?