locked
C# DataGridView: First row is being selected by default RRS feed

  • Question

  • Hai All, 

    I have a WinForm which is having several tabs placed on it and one of the tab is having a DataGridView control which is ReadOnly always. The main problems is, datagridview control will select first row by default every time. I would expect nothing to be selected. 

    I have tried clearing selection after data-binding. But, same behavior. Please suggest me to address my problem. 

    Below is the code while dataBinding (in the Property Set block):***************

    m_items = value; 
    dataGridView1.AutoGenerateColumns = false; 
    bindingSource.DataSource = null; 
    bindingSource.DataSource = m_items; 
    dataGridView1.DataSource = bindingSource; 
    dataGridView1.Refresh(); 

    Below are my DataGridView Settings:******************

    this.dataGridView1.MultiSelect = false; 
    this.dataGridView1.Name = "dataGridView1"; 
    this.dataGridView1.ReadOnly = true; 
    this.dataGridView1.RowHeadersWidth = 20;
    this.dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing; 
    this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; 
    this.dataGridView1.Size = new System.Drawing.Size(263, 130); 
    this.dataGridView1.TabIndex = 0; 
    this.dataGridView1.Enter += new System.EventHandler(this. dataGridView1_Enter); 
    this.dataGridView1.Leave += new System.EventHandler(this. dataGridView1_Leave); 
    this.dataGridView1.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.Grid_CellValidating); 
    this.dataGridView1.SelectionChanged += new System.EventHandler(this.Grid_SelectionChanged);

    • Edited by Kumar... _ Tuesday, February 23, 2010 1:59 PM Formatted
    Tuesday, February 23, 2010 1:52 PM

Answers

  • Hi Kumar,

    Try:

    dataGridView1.CurrentCell.Selected = false;

    Hope this helps!

    Sincerely,
    Helen ZHou
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, March 2, 2010 9:48 AM
    Monday, March 1, 2010 9:31 AM
  • So wired. Ok, I provide some information about my program. You can make these to compare yours.

    First all,
        dgvInputGoods.DataSource = ds.Tables[0]; // datagridview databinding
        dgvInputGoods.ClearSelection(); //clear select first row

    these two lines must at final in form_load event.

    Second, I paste my datagridview code as following
                // dgvInputGoods
                // 
                this.dgvInputGoods.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.dgvInputGoods.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
                this.dgvInputGoods.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dgvInputGoods.Location = new System.Drawing.Point(3, 186);
                this.dgvInputGoods.Name = "dgvInputGoods";
                this.dgvInputGoods.RowTemplate.Height = 23;
                this.dgvInputGoods.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
                this.dgvInputGoods.ShowCellToolTips = false;
                this.dgvInputGoods.Size = new System.Drawing.Size(734, 166);
                this.dgvInputGoods.TabIndex = 5;

    Hope these can help you. :)

    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    • Marked as answer by Kumar... _ Thursday, March 18, 2010 6:37 AM
    Wednesday, February 24, 2010 8:18 AM
  • Hi

    Kumar Helen Zhou is right . It will work.
    Thanks Avinash Desai
    • Marked as answer by Kumar... _ Thursday, March 18, 2010 6:37 AM
    Monday, March 1, 2010 9:58 AM

All replies

  •         private void frmMain_Load(object sender, EventArgs e)
            {

                 ...code...

                dgvInputGoods.DataSource = ds.Tables[0]; // datagridview databinding
                dgvInputGoods.ClearSelection(); //clear select first row

              
            }
    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    Wednesday, February 24, 2010 1:22 AM
  • Huan Li, Thanks for the reply

    Yes, I have tried it by clearing selection as below. 

    m_items = value; 
    dataGridView1.AutoGenerateColumns = false; 
    bindingSource.DataSource = null; 
    bindingSource.DataSource = m_items; 
    dataGridView1.DataSource = bindingSource; 
    dataGridView1.ClearSelection();

    Even then I could see one row being selected. Problem still exists.

    FYI...dataGridView's Enter, Leave and SelectionChanged events have been raised in my code. Is there any event which causes control to select first row?

    Wednesday, February 24, 2010 5:14 AM
  • So wired. Ok, I provide some information about my program. You can make these to compare yours.

    First all,
        dgvInputGoods.DataSource = ds.Tables[0]; // datagridview databinding
        dgvInputGoods.ClearSelection(); //clear select first row

    these two lines must at final in form_load event.

    Second, I paste my datagridview code as following
                // dgvInputGoods
                // 
                this.dgvInputGoods.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.dgvInputGoods.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
                this.dgvInputGoods.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dgvInputGoods.Location = new System.Drawing.Point(3, 186);
                this.dgvInputGoods.Name = "dgvInputGoods";
                this.dgvInputGoods.RowTemplate.Height = 23;
                this.dgvInputGoods.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
                this.dgvInputGoods.ShowCellToolTips = false;
                this.dgvInputGoods.Size = new System.Drawing.Size(734, 166);
                this.dgvInputGoods.TabIndex = 5;

    Hope these can help you. :)

    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    • Marked as answer by Kumar... _ Thursday, March 18, 2010 6:37 AM
    Wednesday, February 24, 2010 8:18 AM
  • Kumar,

    your issue may be related to the bindingsource.  i believe by default its position will be fist record by default.  you cannot set the position to -1 like other collections such as listbox, etc... but you can remove the currentcell of the datagridview.  this will clear the selection in the datagridview but your first row will still be the position of the bindingsource.

    try this

    yourdatagridview.currentcell = nothing

    this should clear it on screen.  depending on your needs however you may still have to adjust the position of the bindingsource.  not sure what your end goal is for this.
    FREE
    DEVELOPER TOOLS     CODE     PROJECTS

    DATABASE CODE GENERATOR
    DATABASE / GENERAL  APPLICATION TUTORIAL
    Upload Projects to share or get help on and post the generated links here in the forum
    www.srsoft.us
    Wednesday, February 24, 2010 2:05 PM
  • Windows Forms Data Binding will never accept nothing selected if the bound collection has at least one item.  This can be verified by trying to set the Position property of the BindingContext provided by the parent form.  If you try to set to -1, it will just acquire a value of zero.  It is just not possible to have no selection in Windows Forms Data Binding, except by having an empty data source.
    MCP
    Thursday, February 25, 2010 1:54 PM
  • Hi Kumar,

    Try:

    dataGridView1.CurrentCell.Selected = false;

    Hope this helps!

    Sincerely,
    Helen ZHou
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, March 2, 2010 9:48 AM
    Monday, March 1, 2010 9:31 AM
  • Hi

    Kumar Helen Zhou is right . It will work.
    Thanks Avinash Desai
    • Marked as answer by Kumar... _ Thursday, March 18, 2010 6:37 AM
    Monday, March 1, 2010 9:58 AM
  • I have resolved my issue. Thanks for all your help....
    Thursday, March 18, 2010 6:38 AM
  • Would you please tell us how you resolved the issue?

    Friday, October 22, 2010 2:52 PM
  • I realize this is an old thread; but a resolution was never posted.

    I found this thread because I was facing the same issue; what I had to do was use the event "DataBindingComplete", in that even simply put datagridview.ClearSelection()  (where datagridview is the name of your grid)

    Hope that helps someone.

    Friday, September 23, 2011 2:55 PM
  • Thank you very much JD. I think thats exactly what most of us were looking for.  Your solution worked first time for me, after some time of frustration before I found it :-D.

     

    Again, thanks a lot for posting your "late reply". It porved to be very useful.


    As long as you live in dreamland, you'll always be divorced from reality.
    Monday, October 3, 2011 2:37 PM
  • But it still generate SelectionChanged event
    Thursday, November 10, 2011 8:40 AM
  • Yes, logic would make me assume it will raise that "SelectionChanged" event cos when you clear selection you are actually changing the selected cell (or row, depending on your selection mode). So basically I dont see that to be a problem. In fact, that would be expected.
    "As long as you live in dreamland, you'll always be divorced from reality." - Mbongo M. Mpongwana
    Monday, November 21, 2011 2:27 PM
  • try to unbind the selection event like this...

     

    DataGridView1.SelectionChanged -= DataGridView1_SelectionChanged;

    DataGridView1.DataSource = bindingSource;

    DataGridView1.ClearSelection();

    DataGridView1.SelectionChanged += new System.EventHandler(DataGridView1_SelectionChanged);

     

    then bind again...

    Friday, December 16, 2011 8:38 AM
  • Just handle the DatabindingComplete event of the datagridview like this:

    private void datagridview1_DataBindingComplete(System.Object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
    {
    datagridview1.ClearSelection();
    }

    • Proposed as answer by JMRO20 Monday, April 9, 2012 9:22 PM
    Monday, April 9, 2012 9:20 PM
  • Found this useful as well. Solved my problem.
    Wednesday, October 28, 2015 8:34 PM
  • You're a god
    Monday, February 5, 2018 7:47 PM