码迷,mamicode.com
首页 > 数据库 > 详细

WinForm----DataGridview---连接数据库,以及双击一条数据,显示信息到Label控件,也可以是TextBox控件。

时间:2015-04-02 23:49:52      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

 

最终效果:

技术分享

 

 

 

 

代码:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Configuration;
11 using System.Data.SqlClient;
12 
13 namespace Test
14 {
15     public partial class Form1 : Form
16     {
17         string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
18 
19         public Form1()
20         {
21             InitializeComponent();
22 
23             data();
24         }
25 
26         public void data()
27         {
28             using (SqlConnection con = new SqlConnection(constring))
29             {
30                 con.Open();
31 
32                 string Sql = "select * from tb_Frinfo";
33 
34                 DataTable dt = new DataTable();
35 
36                 SqlDataAdapter dap = new SqlDataAdapter(Sql, con);
37 
38                 dap.Fill(dt);
39 
40                 this.dataGridView1.DataSource = dt;
41             }
42         }
43 
44         private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
45         {
46             this.label1.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
47             this.label2.Text = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
48             this.label3.Text = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
49             this.label4.Text = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
50             this.label5.Text = this.dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
51             this.label6.Text = this.dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
52             this.label7.Text = this.dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
53             this.label8.Text = this.dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
54         }
55 
56     }
57 }

 

WinForm----DataGridview---连接数据库,以及双击一条数据,显示信息到Label控件,也可以是TextBox控件。

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4388652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!