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