开发者

Unselect all rows in datagridview

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.Ro开发者_StackOverflowws.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!


dataGridView1.ClearSelection();

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


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


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


Just use:

dgvName.ClearSelection();


I tried this and it's working for me:

for (int i = 0; i < dataGridView1.Rows.Count;i++)
{
  dataGridView1.Rows[i].Selected = false;
}


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.


// no selection in dgv at the begening   
dgv.FirstDisplayedCell = null;
dgv.ClearSelection();
// try this it is working !


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜