码迷,mamicode.com
首页 > Windows程序 > 详细

C# winform dataGridView

时间:2014-04-29 16:34:46      阅读:672      评论:0      收藏:0      [点我收藏+]

标签:winform   datagridview   style   blog   http   java   

mamicode.com,码迷
 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("select * from tbl_Student", con);
10                 SqlDataReader dr = cmd.ExecuteReader();
11 
12                 //List<ERPUser> SList=ERPUser.GetAllList();
13 
14                 while (dr.Read()) 
15                 {
16                     //ListBox1
17                     listBox1.Items.Add(dr.GetValue(0) + "  " + dr.GetValue(1) + " " + dr.GetValue(2));
18 
19                     //dataGridView1 绑定数据源 输出列表
20                     
21                 }
22                 dr.Close();
23 
24 
25             }
26             catch (Exception exc)
27             {
28                 MessageBox.Show(exc.Message);
29             }
30             finally
31             {
32                 con.Close();
33             }
34         }
mamicode.com,码迷
mamicode.com,码迷
 1 private void btnClear_Click(object sender, EventArgs e)
 2         {
 3             listBox1.Items.Clear();
 4         }
 5 
 6         private void Form1_Load(object sender, EventArgs e)
 7         {
 8             // TODO: 这行代码将数据加载到表“dBTestDataSet.s1”中。您可以根据需要移动或移除它。
 9             this.s1TableAdapter.Fill(this.dBTestDataSet.s1);
10 
11         }
12 
13         private void button1_Click(object sender, EventArgs e)
14         {
15             //定义数据库连接字段
16             //string str_con = @"Data Source=.;Integrated Security=SSPI;Initial Catalog=DBTest;";
17             string str_con = "server=.;database=DBTest;uid=sa;pwd=sa;";
18             //定义SQL语句字符串
19             string sql = "select * from s1";
20             //定义并实例化数据库连接类型变量
21             SqlConnection conn = new SqlConnection(str_con);
22 
23             conn.Open();
24 
25             SqlCommand cmd = new SqlCommand(sql, conn);
26             SqlDataAdapter da = new SqlDataAdapter(cmd);
27 
28             DataSet ds = new DataSet();
29             da.Fill(ds, "s1");
30 
31             dataGridView2.DataSource = ds;
32             dataGridView2.DataMember = "s1";
33 
34             dataGridView2.Columns[0].HeaderText = "序号";
35             dataGridView2.Columns[0].Width = 90;
36 
37             //设置dataGridView1控件第一列的列头文字
38             dataGridView2.Columns[1].HeaderText = "学号";
39             //设置dataGridView1控件第一列的列宽
40             dataGridView2.Columns[1].Width = 80;
41 
42 
43             dataGridView2.Columns[2].HeaderText = "姓名";          
44             //dataGridView2.Columns[1].DataPropertyName = ds.Tables[0].Columns[1].ToString();
45             dataGridView2.Columns[2].Width = 100;
46 
47 
48             dataGridView2.Columns[3].HeaderText = "性别";
49             //dataGridView2.Columns[2].DataPropertyName = ds.Tables[0].Columns[2].ToString();
50             dataGridView2.Columns[3].Width = 60;
51 
52 
53             dataGridView2.Columns[4].HeaderText = "年龄";            
54             //dataGridView2.Columns[3].DataPropertyName = ds.Tables[0].Columns[3].ToString();
55             dataGridView2.Columns[4].Width = 60;
56 
57 
58             //dataGridView2.Columns[5].HeaderText = "所属科目";
59             //dataGridView2.Columns[4].DataPropertyName = ds.Tables[0].Columns[4].ToString();
60             //dataGridView2.Columns[5].Width = 150;
61             
62             conn.Close();
63 
64         }
mamicode.com,码迷

mamicode.com,码迷

 

表S1只有5列,我想在dataGridView2的第一列 插入序号。

但是不成功~~~~ 怎么弄?

C# winform dataGridView,码迷,mamicode.com

C# winform dataGridView

标签:winform   datagridview   style   blog   http   java   

原文地址:http://www.cnblogs.com/Revel/p/3699560.html

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