Winform tip: display the value of the primary key table in the datagridview.

Source: Internet
Author: User
-

When a project is bound to a datagridview, a field in the table is the primary key of the primary key table, but you want to display the value corresponding to the primary key, for example: when designing a customer management project, the customer information table contains the level number field in the customer level table. Each level number in the customer level table corresponds to a level description. The following describes the specific practices:

In the customer management interface, the datagridview binds the KH table, including fields: customer category (primary key ID bound to the customer type table), customer Company, and customer phone number ......

The category in the interface uses the combox control to bind the Category Table primary key ID. The name of the combox control is lb_cb and the display value is the field lbmc. The binding method is as follows:

 

Code
String SQL =   " Select * from LB " ;
Sqldataadapter da =   New Sqldataadapter (SQL, constr );
Datatable dt =   New Datatable ();
Lb_cb.datasource = DT;
Lb_cb.displaymember =   " Lbmc " ;
Lb_cb.valuemember =   " ID " ;

The connection string constr can be read from the config or written directly into the class.

The name of the datagridview control in the interface is the customer information table bound to dgv.CodeAs follows:

1 String SQL =   " Select * From KH " ;
2 Sqldataadapter da =   New Sqldataadapter (SQL, constr );
3 Datatable dt =   New Datatable ();
4 Dgv. datasource = Da. Fill (DT );

Now, set the display mode for the columns that display customer categories in the datagridview:

The columtype attribute and displaystyle attribute are changed as follows:

Add the following code to the method for displaying data content:

Change the column type of the datagridview
1 String SQL =   " Select * from LB " ;
2 Sqldataadapter da =   New Sqldataadapter (SQL, constr );
3 Datatable dt =   New Datatable ();
4 (Datagridviewcomboboxcolumn) dgv. Columns [ 1 ]). Datasource = Da. Fill (DT );
5 (Datagridviewcomboboxcolumn) dgv. Columns [ 1 ]). Displaymember =   " Lbmc " ;
6 (Datagridviewcomboboxcolumn) dgv. Columns [ 1 ]). Valuemember =   " ID " ;

When you click the dgv column, you want to display this record as follows:

Code
1   Private   Void Dgv_cellclick ( Object Sender, datagridviewcelleventargs E)
2 {
3 If (E. rowindex >   - 1 )
4 {
5 ID = Dgv. Rows [E. rowindex]. cells [ 0 ]. Value. tostring ();
6 Lb_cb.selectedvalue = Dgv. Rows [E. rowindex]. cells [ 1 ]. Value;
7 Gs_tb.text = Dgv. Rows [E. rowindex]. cells [ 2 ]. Value. tostring ();
8 Sj_tb.text = Dgv. Rows [E. rowindex]. cells [ 3 ]. Value. tostring ();
9 Yx_tb.text = Dgv. Rows [E. rowindex]. cells [ 4 ]. Value. tostring ();
10 Fzr_tb.text = Dgv. Rows [E. rowindex]. cells [ 5 ]. Value. tostring ();
11 }
12 }
13

 

 

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.