Unselect all rows in datagridview
Asked Answered
M

9

31

I have two datagridviews, and when I click to one of them, I would like to deselect all selection in the second datagridview, I tried this, but nothing works:

firstItemsDataGridView.ClearSelection();
firstItemsDataGridView.CurrentCell = null;

not working,

firstItemsDataGridView.ClearSelection();
if (firstItemsDataGridView.Rows.Count > 0)
    firstItemsDataGridView[1, 0].Selected = true;
firstItemsDataGridView.CurrentCell = null;
firstItemsDataGridView.ClearSelection();
foreach (DataGridViewRow item in firstItemsDataGridView.Rows) {
    item.Selected = false;

    foreach (DataGridViewCell itemCell in firstItemsDataGridView.Columns) {
        itemCell.Selected = false;
    }
}

not working,

firstItemsDataGridView.Rows[0,-1].Selected = true;

not working too.

I have set selecting mode to full row selection, and I have no idea how to achieve my goal.
thanks a lot!

Matronna answered 6/9, 2011 at 21:8 Comment(3)
is firstItemsDataGridView the desired grid ? Are you assigning a DataSource again ?Esme
firstItemsDataGridView is grid where I need to unselect all items, I am not using any datasources(binding)Matronna
Setting CurrentCell=null should do the trick, but then how is the data shown in the grid provided/added ?Esme
C
82
dataGridView1.ClearSelection();

Should work. Maybe you have code that auto selects rows which is triggered?

Cassaundra answered 6/9, 2011 at 21:14 Comment(3)
Don't call it too soon!Kaine
Putting it in the dataGridView1.MouseDown event works greatCerelly
I was programmatically adding columns and rows and couldn't get this to work. Turns out it needs to be called AFTER doing all of that (so not just after InitializeComponent();).Mesocarp
W
6

An answer at NullSkull solved the problem for me which was that the cell at row 0, column 0 for the datagridview control was always selected on form load, and could not be deselected despite calling ClearSelection within the form's constructor method. I just had to call ClearSelection in the Form_Load event. http://www.nullskull.com/q/10066166/how-to-deselect-the-first-row-when-the-form-i-loaded-in-datagridview-in-windows-application.aspx

Wilkerson answered 7/10, 2013 at 19:38 Comment(0)
C
4

Since there is no answer, and I used this answer in other posts and i ran into the same issue of first row being selected and deselecting wasn't possible:

Color blue  = ColorTranslator.FromHtml("#CCFFFF");
Color red = ColorTranslator.FromHtml("#FFCCFF");
Color letters = Color.Black;

foreach (DataGridViewRow r in datagridIncome.Rows)
{
    if (r.Cells[5].Value.ToString().Contains("1")) { 
        r.DefaultCellStyle.BackColor = blue;
        r.DefaultCellStyle.SelectionBackColor = blue;
        r.DefaultCellStyle.SelectionForeColor = letters;
    }
    else { 
        r.DefaultCellStyle.BackColor = red;
        r.DefaultCellStyle.SelectionBackColor = red;
        r.DefaultCellStyle.SelectionForeColor = letters;
    }
}

This is a small trick, the only way you can see a row is selected, is by the very first column (not column[0], but the one therefore). When you click another row, you will not see the blue selection anymore, only the arrow indicates which row have selected.

SOLUTION:

i found out why my first row was default selected and found out how to not select it by default.

By default my datagridview was the object with the first tab-stop on my windows form. Making the tab stop first on another object (maybe disabling tabstop for the datagrid at all will work to) disabled selecting the first row

Cento answered 19/7, 2013 at 22:7 Comment(1)
dataGridView1.ClearSelection();Pinworm
G
1

Just use:

dgvName.ClearSelection();

Galanti answered 20/12, 2014 at 18:32 Comment(0)
P
1

I tried this and it's working for me:

for (int i = 0; i < dataGridView1.Rows.Count;i++)
{
  dataGridView1.Rows[i].Selected = false;
}
Paola answered 2/5, 2018 at 7:48 Comment(0)
C
0

Make sure all the rows are deselected (dataGridView.Rows[...].Selected = false)

Row zero defaults to selected, so set dataGridView.Rows[0].Selected = false when opening the DataGridView and as long as the other options are set so the user can't select, then you will have, and maintain, nothing selected.

Cropper answered 12/3, 2013 at 23:35 Comment(0)
S
0
// no selection in dgv at the begening   
dgv.FirstDisplayedCell = null;
dgv.ClearSelection();
// try this it is working !
Scup answered 8/9, 2013 at 21:17 Comment(0)
S
0

If your using WPF and want to maintain a MVVM design pattern you can bind both selected items to the same property so when you select one the other will automatically be deselected.

try something like this

public class SingletonClass
{
    private static SingletonClass _Instance;
    public static SingletonClass Instance
    {
        get
        {
            if (_Instance == null)
                _Instance = new SingletonClass();
            return _Instance;
        }
    }   // End Property Instance

    private object _SelectedItem;
    public object SelectedItem
    {
        get { return _SelectedItem; }
        set { _SelectedItem = value; }
    } // End Selected Item
 }

<DataGrid Name="Datagrid1" SelectedItem="{Binding Source={x:Static Const:SingletonClass.Instance},Path=SelectedItem,IsAsync=True}">

<DataGrid Name="Datagrid2" SelectedItem="{Binding Source={x:Static Const:SingletonClass.Instance},Path=SelectedItem,IsAsync=True}">

*I only used a singleton class because this will work across different views in different instances, you can use a regular class if your in the same view.

Please note the IsAsync=True on the xmal, if you plan on using a singleton class across diffrent views it will not work without it.

Stonemason answered 8/4, 2014 at 23:37 Comment(0)
I
0

As said @Taw in subcomments to top answer - "Don't call it too soon!".

In my case default behavior not works at all. My case - datagrid in tab of tabControl and it did not work if that tab not shown before!

That hack works like a charm :

AnyTabControl.SelectedTab = FirsTab;
gridModules.ClearSelection(); //placed at first tab
AnyTabControl.SelectedTab = SecondTab; //that tab i would like to show first

As a result : second tab displayed to user, and when he clicked to first - selection will not appear.

Intradermal answered 5/8, 2016 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.