views:

87

answers:

3

I have a WinForm DataGridView that when I populate it with data the first item is selected by default. I don't want this to occur as to when an item is selected an event fires and some code is executed. Solutions I have for this is to unhook-bind-rehook the event or have a flag that is changed the first time the event fires after a new databind. However I was wondering if there is something a little more elegant.

+1  A: 

I don't have a winforms app open to test, but I'm curious if you have an intervening BindingSource between your data and the datagridview? If so, what if you set

List<Data> data = GetMyData();
BindingSource myBindingSource = new BindingSource();
myBindingSource.DataSource = data;
myBindingSource.position = -1;
myGrid.DataSource = myBindingSource;

I often find it helpful to intervene a BindingSource object between the data and UI. It seems to help fix a lot of random issues, although I'm more accustomed to using DataTable objects as data rather than List<> objects.

Clyde
My Binding Source is a generic List<>
jwarzech
Right, but you can always intervene a BindingSource object -- give me a sec and I'll update the answer with what I mean
Clyde
A: 

Use something like the following example:

        dataGridView.Columns[0].Selected = false;
        dataGridView.Rows[0].Selected = false;
        dataGridView.Rows[0].Cells[0].Selected = false;

Of course, check if there are rows, columns, and so on. It's just an example.

FerranB
+1  A: 

What about (sorry, VB.NET but I'm sure you could convert):

myGrid.ClearSelection()
kevinw