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 interface is bound to the kh table, including the fields: customer category bound to the primary key id of the customer type table), customer company, customer phone number ...... 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border = "0" alt = "" src = "http://www.bkjia.com/uploads/allimg/131228/1U4491Y1-0.jpg"/> 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 Code.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> string SQL =" select * from lb ";
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> SqlDataAdapter DA = new SqlDataAdapter (SQL, Constr );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> DataTable DT = new DataTable ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> LB_CB.DataSource = DT;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> LB_CB.DisplayMember =" lbmc ";
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 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. The binding code is as follows: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 1 string SQL =" select * from kh ";
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 2 SqlDataAdapter DA = new SqlDataAdapter (SQL, Constr );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 3 DataTable DT = new DataTable ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 4 DGV. dataSource = DA. fill (DT); now set the display mode of the columns in the Datagridview datagrithat display customer categories: Change the columtype attribute and DisplayStyle attribute value as follows: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border = "0" alt = "" src = "http://www.bkjia.com/uploads/allimg/131228/1U44943a-11.jpg"/> Add the following code to the method for displaying data content: Change the column type of the dview
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 1 string SQL =" select * from lb ";
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 2 SqlDataAdapter DA = new SqlDataAdapter (SQL, Constr );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 3 DataTable DT = new DataTable ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 4 (DataGridViewComboBoxColumn) DGV. columns [1]). dataSource = DA. fill (DT );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 5 (DataGridViewComboBoxColumn) DGV. Columns [1]). DisplayMember =" lbmc ";
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 6 (DataGridViewComboBoxColumn) DGV. columns [1]). valueMember = "id"; when you click the DGV column, you want to display this record as follows: Code
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 1 private void DGV_CellClick (object sender, DataGridViewCellEventArgs e)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 2 {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 3 if (e. RowIndex>-1)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 4 {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 5 id = DGV. rows [e. rowIndex]. cells [0]. value. toString ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 6 LB_CB.SelectedValue = DGV. Rows [e. RowIndex]. Cells [1]. Value;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 7 gs_TB.Text = DGV. rows [e. rowIndex]. cells [2]. value. toString ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 8 sj_TB.Text = DGV. rows [e. rowIndex]. cells [3]. value. toString ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 9 yx_TB.Text = DGV. rows [e. rowIndex]. cells [4]. value. toString ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif"/> 10 fzr_TB.Text = DGV. rows [e. rowIndex]. cells [5]. value. toString ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 11}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 12}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1U44912T-1.gif "/> 13

This article is from the "ye Wenwen" blog. For more information, contact the author!

Related Article

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.