The position of bindingsource is equal to the selected row method of the datagridview.

Source: Internet
Author: User

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!

 

 

 

 

 

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.