5

How to make a datagrid view cell not selected at form load for this

I have tried too much

my dgvproducts properties are (readonly = false,selection mode = CellSelect)

1) i have place this code in form shown event but that does not work for me ..

         dgvProducts.Clearselection();

2) I have place the above code in databinding event like the below..

     private void dgvProducts_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
     {
         //dgvProducts.ClearSelection();
         ((DataGridView)sender).ClearSelection();
     }

it does not work for me ...

3) i have placed similar code and i have added extra line to that in form load event but does not work for me ..

  dgvProducts.ClearSelection();
  dgvProducts.currentcell = null;

but this is not work for me ....

this is my form load code

      private void SellEquipment_Load(object sender, EventArgs e)
      {
            getProductDetails();
            dgvProducts.Columns[0].Visible = false;

            for (int i = 0; i < dgvProducts.Columns.Count; i++)
            if (dgvProducts.Columns[i] is DataGridViewImageColumn)
            {
                ((DataGridViewImageColumn)dgvProducts.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
                break;
            }
      }

and this is my getproductdetails code

   private void getProductDetails()
   {
        var products = from productlist in dbcontext.products
                       select new
                       {
                           productid = productlist.productId,
                           Name = productlist.Name,
                           Image = productlist.Image,
                           Description = productlist.Description,
                           Price = productlist.Price
                       };

        BindingProductsource.DataSource = products;
        dgvProducts.DataSource = BindingProductsource;
        dgvProducts.ClearSelection();         
   }

would any one pls help on this..

Many thanks...

Glory Raj
  • 17,397
  • 27
  • 100
  • 203

2 Answers2

5

Try creating a new event OnShow and do this code:

    protected override void OnShown(EventArgs e)
    {
        if (this.dataGridView1.SelectedCells.Count > 0)
        {
            for (int i = 0; i < this.dataGridView1.SelectedCells.Count; i++)
                this.dataGridView1.SelectedCells[i].Selected = false;
        }
    }
Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
0

I found that overriding Form.OnShown() as advised in the answer by @Mitja Bonca indeed worked for my purposes. However, I found that making use of DataGridView.ClearSelection() allowed me to achieve a leaner solution:

protected override void OnShown(EventArgs e)
{
    this.dataGridView1.ClearSelection();
    base.OnShown(e);
}

Regarding the call to base.OnShown(), in its documentation Microsoft advises:

Notes to Inheritors

When overriding OnShown in a derived class, be sure to call the base class's OnShown method so that registered delegates receive the event.

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191