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

最终效果:

代码:

 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 }
时间: 2024-08-05 11:14:10

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

Winform 多个窗口编辑同一条数据同步的实现

场景: 一个主窗口中,可以在列表(DataGridView)里选中一条记录编辑,打开一个编辑窗口(非模态窗口),编辑窗口保存后需要刷新父窗口,由于编辑窗口是非模态窗口,如果打开了多个窗口,并且都是编辑同一条数据,那么一个窗口保存(并关闭)后,需要通知其它正在打开的窗口"数据有更改,需要刷新" 首先,刷新父窗口,如果是打开编辑窗口是模态窗口,那么可以类似如下的实现(伪代码): FormEdit frm = new FormEdit(); frm.EditId = 选中数据行对应的id;

SQL 去重 显示第一条数据 显示一条数据

需求描述:根据某一个字段或几个字段去重来显示任一条数据,第一条或最后一条. 数据样式如下图: 尝试解决: --count(*)方法(只把条数为1条的显示出来了,超过1条全部过滤了) select * from t4 where 自编条码 in (select 自编条码 from t4 group by 自编条码 having count(id)=1) 以上方法,会把仅1条记录的显示,但是重复的并没有保留其中一条,也过滤掉了. 所以,我们需要变化一下,提供三种解决方法: 一.通过row_numbe

C# winform DataGridView 常见属性

C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删除 ⑦ 禁止列或者行的Resize ⑧ 列宽和行高以及列头的高度和行头的宽度的自动调整 ⑨ 冻结列或行 ⑩ 列顺序的调整 ⑪ 行头列头的单元格⑫ 剪切板的操作 ⑬ 单元格的ToolTip的设置 ⑭ 右键菜单(ContextMenuStrip)的设置 ⑮ 单元格的边框. 网格线样式的设定 ⑯ 单元格表

C# winform dataGridView

1 private void btnConn_Click(object sender, EventArgs e) 2 { 3 //定义连接字符串 4 string constr = "server=.;database=DBTest;uid=sa;pwd=sa;"; 5 SqlConnection con = new SqlConnection(constr); 6 try 7 { 8 con.Open(); 9 SqlCommand cmd = new SqlCommand(&quo

C# WinForm DataGridView界面设计小技巧

在窗口中表格是非常常见的数据显示形式,所以界面的展示效果非常重要,通过多次的使用之后发现C# WinForm DataGridView控件默认的显示样式非常之反人类,不过好在可视化操作只需几个简单的属性修改就能得到很好的效果. 下面请看生成的默认属性的DataGridView显示: 非常之反人类的表格. 开始修改下面几个通过名字就能够读懂的属性(当然是通过VS属性窗口修改,也可以在初始化代码中手动修改DataGridView的属性): AllowUserToAddRows=False; //不同

使用JDBC向数据库中插入一条数据

原谅我是初学者,这个方法写的很烂,以后不会改进,谢谢 /** * 通过JDBC向数据库中插入一条数据 1.Statement 用于执行SQL语句的对象 1.1 通过Connection 的 * createStatement() 方法来获取 1.2 通过executeUpdate(sql) 的方法来执行SQL 1.3 * 传入的SQL可以是INSERT/UPDATE/DELETE,但不能是SELECT * * 2.Connection和Statement使用后一定要记得关闭 需要在finally

解决hibernate只能插入一条数据的问题

hibernate初学,根据视频教程写好代码后,发现无论执行多少次main函数,数据库中只有一条数据,尝试多次,后来终于发现问题... 使用的工具是:MYSQL 5.7.13   eclipse 4.5.2  hibernate 4.0.5 第一步: 在mysql中新建一个数据库 名为DEMO01,然后再创建一个test表: USE DEMO01; CREATE TABLE test( TEST_ID int auto_increment, name varchar(20), TEST_DATE

新建一个DataTable如何手动给其添加多条数据!

早晨起来,想起昨天利用winform做类似于sqlserver数据库导入数据功能的时候,用到了新建一个DataTable手动给其添加多条数据,平时用不到,需要的时候想不起来了,这次不妨把他记下来.以下是代码,很简单. //声明并实例化datatable DataTable dt = new DataTable(); //实例化三个列            DataColumn dc1 = new DataColumn("factoryName",System.Type.GetType(

WinForm DataGridView分页功能

WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 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.T