jwarzech
jwarzech

Reputation: 6665

Preventing a DataGridView from selecting the first item

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.

Upvotes: 2

Views: 2349

Answers (5)

stefankmitph
stefankmitph

Reputation: 3306

Pretty late, but the only I way i got it working for me (a lot of TextBoxes, ComboBoxes, etc. bound to my DataGridView via BindingSource):

After initializing DataBindings to my Controls and loading the data into DataGridView I had to suspend the binding

bindingSource.SuspendBinding();

In my RowHeaderMouseClick event I checked if the binding is suspended. If so, just resume it:

if(bindingSource.IsBindingSuspended)
    bindingSource.ResumeBinding();

This way I can complete the loading and binding of the data (in my case a sortable BindingList), show the data to the user but with no record selected.

Hope this helps at least anybody!

Upvotes: 0

Marco Hansma
Marco Hansma

Reputation: 171

I had a similar problem (but I don't use the SelectionChanged event), and this works for me:

In the constructor, after the binding is set, add a handler to the DataBindingComplete event:

dgvCommandos.DataSource = systeemCommandos; // = my List<> of objects
dgvCommandos.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dgvCommandos_DataBindingComplete);

The handler:

void dgvCommandos_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dgvCommandos.ClearSelection();
}

If you intend to select a row (e.g. after adding/inserting a new object), simply set:

dgvCommandos.Rows[insertPos].Selected = true;

Using this as a basis, I think it is possible to handle other events to react upon user selection. Hope this helps.

Upvotes: 3

kevinw
kevinw

Reputation: 2178

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

myGrid.ClearSelection()

Upvotes: 2

FerranB
FerranB

Reputation: 36767

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.

Upvotes: 0

Clyde
Clyde

Reputation: 8145

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.

Upvotes: 2

Related Questions