最终效果:

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

代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient; namespace Test
{
public partial class Form1 : Form
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString; public Form1()
{
InitializeComponent(); data();
} public void data()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open(); string Sql = "select * from tb_Frinfo"; DataTable dt = new DataTable(); SqlDataAdapter dap = new SqlDataAdapter(Sql, con); dap.Fill(dt); this.dataGridView1.DataSource = dt;
}
} private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.label1.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label2.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label3.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label4.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label5.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label6.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label7.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
this.label8.Text = this.dataGridView1.SelectedRows[].Cells[].Value.ToString();
} }
}
05-06 22:41
查看更多