DataGridView Click on the blank and lose focus, deselect and close the default selection first row C#winform

Source: Internet
Author: User

By default, the first row is selected and can be deselected by calling DataGridView's clearselection in the form's Load event.

You can also set

CurrentCell = null for DataGridView;

You can also dgv a row that has already been selected. Row[0]. Selected=false;

So how do you let the user click on the arrow to cancel the selection?

Need to be handled within the DataGridView MouseDown or MouseClick Event

DataGridView DGV = sender as DataGridView;
DataGridViewRow Clickedrow = Getclickedrow (DGV, e.y);
if (Clickedrow = = null)
{
DGV. ClearSelection ();
}

Getclickedrow is implemented as follows
 PublicDataGridViewRow Getclickedrow (DataGridView DGV,intMousey) {            if(DGV. Firstdisplayedscrollingrowindex <0)            {                return NULL; }            if(DGV. Columnheadersvisible = =true&& Mousey <=DGV. Columnheadersheight) {return NULL; }            intindex =DGV.            Firstdisplayedscrollingrowindex; intDisplayedcount = DGV. Displayedrowcount (true);  for(inti =1; I <=Displayedcount;) {                if(DGV. Rows[index]. Visible = =true) {Rectangle rect= DGV. Getrowdisplayrectangle (Index,true); if(Rect. Top <= mousey && Mousey <rect. Bottom) {returnDGV.                    Rows[index]; } I++; } Index++; }            return NULL; }
View Code


When you do this, click the area in the red box to cancel the selected row.


How to lose focus when the control loses focus (for example, to click on another DataGridView)
private void Dgtwaybillaccountdetialdgv_leave (object sender, EventArgs e)        {            DataGridView DGV = sender As DataGridView;            DGV. ClearSelection ();        } 

DataGridView Click on the blank and lose focus, deselect and close the default selection first row C#winform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.