how to prevent datagridview cell selection at form load

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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

回答1:

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;         }     } 


回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!