Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent datagridview cell selection at form load

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...

like image 953
Glory Raj Avatar asked Sep 07 '11 11:09

Glory Raj


1 Answers

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;
        }
    }
like image 76
Mitja Bonca Avatar answered Oct 12 '22 23:10

Mitja Bonca