tags:

views:

116

answers:

3

Hello!

Does anybody knows how to unselect ALL the rows on a Windows Forms DataGrid control, using C#? By default, the first row is always selected... Also, I don't want to allow any kind of selection; do you guys know any method to this?

I've already searched here but can't find...

Any help would be great!

Cheers!

Edit: Ok, i've found another way to unselect a row; on the DataGridViewRow.RowPostPaint() event, use the Selected property to unselect the row who send the event.

private void grid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    this.gridLogEntries.Rows[e.RowIndex].Selected = false;
}
+2  A: 

I don't think there's a simple solution like DisableSelection = true.

Anyway, handling SelectionChanged event in the following way, should be enough:

private bool skipEvents;

void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (skipEvents)
        return;

    skipEvents = true;

    // disable cell selection
    foreach (DataGridViewCell cell in this.dataGridView1.SelectedCells)
        cell.Selected = false;
    // disable row selection
    foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
        row.Selected = false;
    // disable column selection
    foreach (DataGridViewColumn col in this.dataGridView1.SelectedColumns)
        col.Selected = false;

    skipEvents = false;
}

EDIT:

I slightly changed the code to avoid recursive calls of the method.

digEmAll
Hi digEmAll! I din't tried your sugestion, but I believe it works. Check the code I put on Rewinder answer. Thanks for the help anyway! :)
jmpcm
@jmpcm: this solution, not only unselect first row that's automatically selected on grid creation, but also disable all kind of user/programmatical selections. Just try it ! ;)
digEmAll
@digEmAll: indeed you solution is quite cool! Please, check the edit I've done on my post, it also works fine for what I need. The only thing I don't know is if mine solution is "heavier" than yours because it executes an event for each row inserted... What do you think?
jmpcm
Well, your code works because when a row is selected/clicked (not only when it's inserted) is "RePainted" to apply highlighing color. The code is little heavier than mine, because for example if you minimize and then maximize the form containing the grid, the method is called for each row even if is not necessary. Furthermore, I suppose is much better/clearer/more correct disable selection in event handler concerning selection.
digEmAll
A: 

Have you tried this?

myTable.CurrentRowIndex = -1;
Christian Nesmark
Hi! I can't find that property! But this would be the perfect solution for me (laziness ;) )
jmpcm
A: 

You could try calling the unselect method for each selected row:

        for (int i = 0; i < bindingSource1.Count; i++)
        {
            if (myGrid.IsSelected(i))
            {
                myGrid.UnSelect(i);
            }
        }
Rewinder
Hi Rewinder! The properties you suggested don't exit, but this solution works fine: "foreach (DataGridViewRow row in this.grid.Rows) { row.Selected = false; }" Thanks for the help!
jmpcm
@jmpcm: maybe the code is for a web grid.. anyway, you don't need to iterate over all `grid.Rows`, but just over `grid.SelectedRows` as shown in my answer
digEmAll
Yeah, I haven't thought in that (sometimes I'm silly xD) Thanks for the idea, but now I'm using the code from the 'Edit' I've done on the post. PS: quite probably your suggestion is only applicable to a web grid, but I have no experience with those...
jmpcm
@jmpcm: Actually, the code I posted is for a DataGrid (which you mentioned in your original post), while your code seems to target the DataGridView, which is basically a newer version of the classic DataGrid. See http://msdn.microsoft.com/en-us/library/ms171628.aspx for the difference.
Rewinder
@Rewinder: you're right, it's totally my fault! indeed is a DataGridView but usually I call it by the shorter name. Thanks for the help anyway! :)
jmpcm