I have never used Microsoft's built-in dview, and all of them use developer Express. net v8.2, a third-party control, which is easy to use. It really misses some days. After all, it is a charged item. I will not talk much about it. Let's talk about how to implement it:
In your form_load method, enter the followingCode:
//Set the selection mode of the datagridview.
Gvhistory. selectionmode=Datagridviewselectionmode. fullrowselect;
Bindingsource BS= NewBindingsource ();
Datagridview dgv=NewDatagridview ();
Dgv. datasource=BS;
Add the following code to your cellmouseclick and rowheadermousedoubleclick events. This is the place you need to pay attention to. Set bindingsource. the key to position is what the form of your bindingsource data source is. I tested two formats: datatable and bindinglist <>. These two methods are different, the find and indexof methods for bindingsource are described as follows:
Bindinglist format:
Code
// Bindinglist is returned. < Car >
BS. datasource = doorbll. getbindinglist (ID );
Car C = (CAR) gvhistory. Rows [E. rowindex]. databounditem;
// This method is recommended.
Int A = BS. indexof (gvhistory. Rows [E. rowindex]. databounditem );
// This method actually calls the bindinglistIndexof () method
// Int B = BS. indexof (C );
BS. Position = A;
Car C2 = (CAR) BS. Current;
Datatable format:
Code
// The returned data is the set of datatable.
BS. datasource = doorbll. getdatatable (ID );
Car C=(CAR) gvhistory. Rows [E. rowindex]. databounditem;
IntA=BS. Find ("ID", C. ID );
BS. Position=A;
Car C2=(CAR) BS. Current;
In this way, you can change bindingsource. position value. When you have other controls bound to this data source, other controls will change when you change the current value. This method is also applicable to editing, add a series of methods.
I hope it will help you!