Hello All:

I have 2 dataGridViews and id like it that when I select a row in dgv_A that dgv_B clearsSelection.

I have aceived this using

dgv_A_SelectionChanged
{
         dgv_B.ClearSelection();
}

and the same for dgv_B_SelectionChanged, except it obviously clears A.

This works fine except for one thing :

when I click in the dgv that has no row selcted, it clears the other dgv (as expected) but it also clears itself to (or rather the row I just clicked isnt selected), making me have to click again to select a row.

There is an overload for the clearSelection Method that allows you to pass in the column & row so that it is not cleared, i have tried the following with no success :-

public void dgv_A_SelectionChanged(object sender, EventArgs e)
        {
            dgv_A.ClearSelection(dgv_A.SelectedColumns[0].Index, dgv_A.SelectedRows[0].Index, false);
            dgv_B.ClearSelection();
         }

I get a nice syntax error from intellisense saying:-
ClearSelection(int, int, bool) is unavailable because of its protection level

Can some kind soul explain what im doing wrong.

Many thanks

Tino

Hi,

your problem is that when you clear the selection of a datagrid it fires the SelectionChanged event...so selecting cell in A clears B which then clears A.

Just add a check to ensure the selectionchanged event fired because something was selected:

private void dgv_B_SelectionChanged(object sender, EventArgs e)
        {
            if (dgv_B.SelectedCells.Count > 0)
            {
                dgv_A.ClearSelection();
            }
        }

        private void dgv_A_SelectionChanged(object sender, EventArgs e)
        {
            if (dgv_A.SelectedCells.Count > 0)
            {
                dgv_B.ClearSelection();
            }
        }

this way, when you clear the second grid, the event fires, but the number of selected cells is 0 so it wont clear the first grid.

Ahh I see

clearSelection fires selectionChanged!

Many thnaks Ryshad, I altered it and it works great :-)

where in msdn can i find these little gems as I did have a quick look but it would be nice to know whats firing off.

Kind regards

Tino

Info is here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged.aspx

namely:

This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action.

But to be honest i just knew from experience, i had to check the msdn before posting to see if it mentioned this :)

If you want to know whats going on behind the scenes you can always throw in a breakpoint and step through the code line by line, thats always my first step if something unexpected is happening in my code.

Thank you Ryshad, really appreciated :-)

Allthough its mentioned in the selectionChanged Event docs Its not mentioned in the clearSelction Method docs, I would never have found it!!

It should be in the clearSelction Method docs under some sort of 'events raised when this Method invoked' catagory

Unless it is bobbing around and im missing it!

Your right, it would be helpful to those less versed in .net to know what events are gonna fire :) Maybe you should mention it to Microsoft ;)

But that's all part of learning the language: learning how everything interacts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.