winfrom (1) 窗体传值 连接 (2)鼠标移动窗口
private void button1_Click(object sender, EventArgs e) //这里是不用泛型集合的情况,用的是Datatable { SqlConnection conn = new SqlConnection("server=.;database=Cml;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from student"; conn.Open(); SqlDataReader dr= cmd.ExecuteReader(); DataTable dt = new DataTable(); if(dr.HasRows) { //开始添加列 for (int i = 0; i < dr.FieldCount; i++) //dr.FieldCount 表里的列数 { dt.Columns.Add(dr.GetName(i)); //一列一列的添加,for 循环循环的是确定了某一行之后每一列 } //开始添加行 while (dr.Read()) //while循环的是每一行的数据,一行一行的循环 { DataRow dtw = dt.NewRow(); //造一个新行 //下面开始给新行添加内容 for (int i = 0; i < dr.FieldCount; i++) { dtw[i] = dr[i]; //开始给确定了某一行的每一列依次赋值,for循环循环一次赋一次列值 } //将while循环完事后的每一行的内容全部增加到dt里面 dt.Rows.Add(dtw); } } //现在Datatable里面已经有内容了,然后datagridview1的数据源便是dt dataGridView1.DataSource = dt; cmd.Dispose(); conn.Close(); } 复制代码
--拖动窗体
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace _2015年6月11日wwZ { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool isok; //用来区分鼠标点下去还是松开 //初始坐标 private int ChuShiX; //初始横坐标 private int ChuShiY; //初始纵坐标 //鼠标点下去时候的坐标 private int ShuBiaoDianX;//鼠标点下去横坐标 private int ShuBiaoDianY;//鼠标点下去纵坐标 //移动过程中的坐标 private int YiDongX;//鼠标点下去移动时的横坐标 private int YiDongY;//鼠标点下去移动时的纵坐标 private void Form1_MouseDown(object sender, MouseEventArgs e) //鼠标点下去 { isok = true; ShuBiaoDianX = Cursor.Position.X; //给鼠标点下去横坐标赋值 ShuBiaoDianY = Cursor.Position.Y;//给鼠标点下去纵坐标赋值 //给初始坐标赋值 ChuShiX = this.Location.X; ChuShiY = this.Location.Y; } private void Form1_MouseUp(object sender, MouseEventArgs e) { isok = false; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if(isok) { //给移动时的坐标赋值 YiDongX = Cursor.Position.X; YiDongY = Cursor.Position.Y; //重新给窗体赋上新坐标 this.Location = new Point(ChuShiX+YiDongX-ShuBiaoDianX,ChuShiY+YiDongY-ShuBiaoDianY); } }