Winform中DataGridview的基本用法

 一  dataGridView数据控件的数据绑定和对数据的操作 

      一些基本的功能都已实现  相关下载:DataGridViewTest.rar

 

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 using System;
2   using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Data.SqlClient;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace DataGridViewTest
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 int Fcellvalue;   // 用于保存主键的值
20
21 SqlDataAdapter da;
22
23 SqlConnection conn;
24
25 SqlCommand cmd = new SqlCommand();
26
27 string connstr = " Data Source = .;database=Test;uid=sa;pwd=123; " ;
28
29 private void Form1_Load( object sender, EventArgs e)
30 {
31
32
33 Getdate();
34 dataGridView1.Columns[ 0 ].Visible = false ;
35
36 dataGridView1.Columns[ 1 ].HeaderText = " 姓名 " ;
37 dataGridView1.Columns[ 2 ].HeaderText = " 性别 " ;
38 dataGridView1.Columns[ 3 ].HeaderText = " 民族 " ;
39 dataGridView1.Columns[ 4 ].HeaderText = " 公司 " ;
40 dataGridView1.Columns[ 5 ].HeaderText = " 职位 " ;
41 dataGridView1.Columns[ 6 ].HeaderText = " 地址 " ;
42 }
43
44 private void timer1_Tick( object sender, EventArgs e)
45 {
46 toolStripStatusLabel5.Text = " 当前时间: " + DateTime.Now.ToString();
47 }
48
49 // 绑定数据
50 public void Getdate()
51 {
52 try
53 {
54 conn = new SqlConnection(connstr);
55
56 conn.Open();
57
58 string strcmdsel = " select * from T1 " ;
59
60 da = new SqlDataAdapter(strcmdsel, connstr);
61
62 DataSet ds = new DataSet();
63
64 da.Fill(ds);
65
66 dataGridView1.DataSource = ds.Tables[ 0 ].DefaultView;
67
68 }
69 catch (SqlException sql)
70 {
71 MessageBox.Show(sql.ToString());
72 }
73 catch (Exception ex)
74 {
75 MessageBox.Show(ex.ToString());
76 }
77 finally
78 {
79 conn.Close();
80 }
81
82 }
83
84 // 刷新数据
85 private void button5_Click( object sender, EventArgs e)
86 {
87 Getdate();
88 }
89
90 // 单击行标题的时候 取到这一行第一列的值 即主键的值
91 private void dataGridView1_RowHeaderMouseClick( object sender, DataGridViewCellMouseEventArgs e)
92 {
93 Fcellvalue = Convert.ToInt32(dataGridView1.CurrentRow.Cells[ 0 ].Value); // 取到主键
94 }
95
96 // 查询满足条件的数据
97 private void button1_Click( object sender, EventArgs e)
98 {
99 try
100 {
101
102 if (textBox1.Text.Trim() == "" )
103 {
104 MessageBox.Show( " 请输入查询条件!!! " );
105 }
106 else
107 {
108 conn = new SqlConnection(connstr);
109
110 conn.Open();
111
112 string selectLevel = " select * from T1 where LevelInfo=' " + textBox1.Text.Trim() + " ' " ;
113
114 da = new SqlDataAdapter(selectLevel, connstr);
115
116 DataSet ds = new DataSet();
117
118 da.Fill(ds);
119
120 dataGridView1.DataSource = ds.Tables[ 0 ].DefaultView;
121
122 }
123
124 }
125 catch (SqlException sql)
126 {
127 MessageBox.Show(sql.ToString());
128 }
129 catch (Exception ex)
130 {
131 MessageBox.Show(ex.ToString());
132 }
133 finally
134 {
135 conn.Close();
136 }
137
138 textBox1.Text  =   "" ;
139 }
140
141 // 删除选中行
142 private void button2_Click( object sender, EventArgs e)
143 {
144 try
145 {
146 conn = new SqlConnection(connstr);
147
148 conn.Open();
149
150 string strcmddel = " delete from T1 where ID= " + Fcellvalue;
151
152 cmd.Connection = conn;
153 cmd.CommandText = strcmddel;
154 cmd.CommandType = CommandType.Text;
155
156 cmd.ExecuteNonQuery();
157
158 Getdate();
159 }
160 catch (SqlException sql)
161 {
162 MessageBox.Show(sql.ToString());
163 }
164 catch (Exception ex)
165 {
166 MessageBox.Show(ex.ToString());
167 }
168 finally
169 {
170 conn.Close();
171 }
172
173 }
174
175 // 新增一行数据
176 private void button3_Click( object sender, EventArgs e)
177 {
178
179 try
180 {
181 conn = new SqlConnection(connstr);
182
183 conn.Open();
184
185 string F = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 1 ].Value.ToString();
186 string S = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 2 ].Value.ToString();
187 string T = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 3 ].Value.ToString();
188 string Four = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 4 ].Value.ToString();
189 string Five = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 5 ].Value.ToString();
190 string Six = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 6 ].Value.ToString();
191
192 string strcmdIn = " insert into T1 values(' " + F + " ',' " + S + " ',' " + T + " ',' " + Four + " ',' " + Five + " ',' " + Six + " ') " ;
193 cmd.Connection = conn;
194 cmd.CommandText = strcmdIn;
195 cmd.CommandType = CommandType.Text;
196
197 cmd.ExecuteNonQuery();
198
199 Getdate();
200 }
201 catch (SqlException sql)
202 {
203 MessageBox.Show(sql.ToString());
204 }
205 catch (Exception ex)
206 {
207 MessageBox.Show(ex.ToString());
208 }
209 finally
210 {
211 conn.Close();
212 }
213 }
214
215 // 修改数据项
216 private void button4_Click( object sender, EventArgs e)
217 {
218
219
220 try
221 {
222 conn = new SqlConnection(connstr);
223
224 conn.Open();
225
226 string F = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 1 ].Value.ToString();
227 string S = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 2 ].Value.ToString();
228 string T = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 3 ].Value.ToString();
229 string Four = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 4 ].Value.ToString();
230 string Five = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 5 ].Value.ToString();
231 string Six = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ 6 ].Value.ToString();
232
233 string strcmdUpdate = " update T1 set Name=' " + F + " ',Sex=' " + S + " ', " +
234 " Minzu=' " + T + " ',Com=' " + Four + " ',LevelInfo=' " + Five + " ',Address=' " + Six + " ' where ID = " + dataGridView1.CurrentRow.Cells[ 0 ].Value;
235 cmd.Connection = conn;
236 cmd.CommandText = strcmdUpdate;
237 cmd.CommandType = CommandType.Text;
238
239 cmd.ExecuteNonQuery();
240
241 Getdate();
242 }
243 catch (SqlException sql)
244 {
245 MessageBox.Show(sql.ToString());
246 }
247 catch (Exception ex)
248 {
249 MessageBox.Show(ex.ToString());
250 }
251 finally
252 {
253 conn.Close();
254 }
255 }
256
257 private void dataGridView1_CellClick( object sender, DataGridViewCellEventArgs e)
258 {
259 if (e.RowIndex == - 1 )
260 return ;
261
262 dataGridView1.Rows[e.RowIndex].Cells[ 1 ].Style.ForeColor = Color.Red;
263 dataGridView1.Rows[e.RowIndex].Cells[ 2 ].Style.ForeColor = Color.Red;
264 dataGridView1.Rows[e.RowIndex].Cells[ 3 ].Style.ForeColor = Color.Red;
265 dataGridView1.Rows[e.RowIndex].Cells[ 4 ].Style.ForeColor = Color.Red;
266 dataGridView1.Rows[e.RowIndex].Cells[ 5 ].Style.ForeColor = Color.Red;
267 dataGridView1.Rows[e.RowIndex].Cells[ 6 ].Style.ForeColor = Color.Red;
268 }
269
270 private void dataGridView1_CellLeave( object sender, DataGridViewCellEventArgs e)
271 {
272 if (e.RowIndex == - 1 )
273 return ;
274 dataGridView1.Rows[e.RowIndex].Cells[ 1 ].Style.ForeColor = Color.Black;
275 dataGridView1.Rows[e.RowIndex].Cells[ 2 ].Style.ForeColor = Color.Black;
276 dataGridView1.Rows[e.RowIndex].Cells[ 3 ].Style.ForeColor = Color.Black;
277 dataGridView1.Rows[e.RowIndex].Cells[ 4 ].Style.ForeColor = Color.Black;
278 dataGridView1.Rows[e.RowIndex].Cells[ 5 ].Style.ForeColor = Color.Black;
279 dataGridView1.Rows[e.RowIndex].Cells[ 6 ].Style.ForeColor = Color.Black;
280 }
281
282 }
283 }

转载于:https://www.cnblogs.com/ShuiMu/articles/2001441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值