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

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

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

Winform中DataGridview的基本用法相关推荐

  1. Winform中datagridview显示数据时,不显示特殊符号,如下划线不显示问题

    问题描述: Winform中datagridview显示数据时,不显示特殊符号,如下划线不显示问题 解决问题: 当遇到datagridview单元格数据不显示带有下划线的符号时,是字体设置问题,我们可 ...

  2. winform中DataGridview数据绑定时格式化文本,如:将部分文字改成绿色,部分文字改成红色...

    在winform中,使用DataGridView时,想在某一列,值为"true"时,将这列颜色改变,并且将值也改变,需要用到如下方法: Code private void gdvD ...

  3. Winform中DataGridView设置前景色、单元格背景色、标题栏样式、禁止改变高宽、不显示空白行、清除选中样式、填充数据源、设置标题、设置单列宽度

    场景 Winform中使用DataGridView实现加载数据并显示在led大屏中. 需要设置整个DataGridView的前景色.背景色.单元格颜色.标题栏样式.禁止 改变行高.列宽.不显示新增行. ...

  4. Winform中DataGridView绑定IList数据源后的排序

    首先,实现ICompare接口 public class ObjectPropertyCompare<T> : IComparer<T> { private PropertyD ...

  5. C# Winform中DataGridView的DataGridViewCheckBoxColumn CheckBox选中判断

    1.DataGridViewCheckBoxColumn CheckBox是否选中 在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].F ...

  6. Winform 中 dataGridView 导出到Excel中的方法总结

    最近,在做CS端数据导出到Excel中时网上找了很多代码感觉都不是自己想要的,通过自己的整理归纳得到一个比较通用的方法,就给大家分享一下: 该方法需要用到两个参数(即对象),一个  DataGridV ...

  7. C#Winform中DataGridView控件根据鼠标左右键获取单元格值或者行列

    有多种方法可以获取单元格的值或者行列,此处选用了两种方式去获取分别用了CurrentCell方法和Rows[].Cells[]的方法去获取行列值 鼠标左键获取单独行列值 此处获取左键行列值的触发条件是 ...

  8. Winform中DataGridView中添加数据并且拖动整行

    dgv_senceConfig_sence是DataGridView控件的名称: 这是整段代码的逻辑比较多,需要用户自己去选择,这个可以实现添加数据和拖动整行(肯定实现了) using Control ...

  9. winform中datagridview控件的checkboxcolumn勾选状态失效

    近日,在做一个跨页多选导出的功能时遇到一个问题:我在第一页勾选多条数据,将勾选数据的唯一标识存储在变量中,跳转到第二页,也勾选几条数据,将勾选数据的唯一标识存储在变量中,然后再跳转回第一页,并根据变量 ...

最新文章

  1. joomla源代码探析(五) framework.php
  2. 问题征集 | 跟计算机史学家对谈是一种怎样的感受
  3. AFNetworking和ASIHTTPRequest的比较
  4. 用Linux虚拟机手工编译安装Apache
  5. mysql dump 增量_mysql mysqldump数据备份和增量备份
  6. html css js实现快递单打印_JS与HTML、CSS实现2048小游戏(六)
  7. 奇异值分解讨论及其实现的计算步骤
  8. python获取视频时长方法
  9. 局域网聊天 一个十分热门的话题
  10. php页面文件后缀名,PHP中获取文件扩展名的N种方法
  11. es用python增加字段_使用Python在ElasticSearch中添加@timestamp字段
  12. iphone pageController 的实现
  13. 统一社会信用代码解析登记管理部门和机构类别
  14. html+css练手项目3
  15. Wireguard 全互联模式(full mesh)权威指南
  16. navigator、history对象
  17. 算法基础—数据结构—双链表
  18. 要善于借势破局——宁向东的清华管理学课第4课
  19. 【数据结构】最小瓶颈路 加强版(Kruskal重构树RMQ求LCA)
  20. 请帮我写一封情书,500字左右

热门文章

  1. c++基础知识第十天:结构体嵌套结构体,结构体作函数参数
  2. 都昌 DCWriter电子病历编辑器演示文档截屏
  3. SAP部分支付和剩余支付清账的区别
  4. 某公司企业网络服务器的构建
  5. 832计算机专业基础,2020年福建师范大学832计算机应用综合专业硕士研究生入学考试大纲...
  6. MyBatis实现模糊查询
  7. DeepLearning | 经典卷积神经网络Alex_Net (完整模型与tensorflow代码讲解,附数据集)
  8. html表单怎么自动换行,html表单怎么换行 首先新建一个HTML文件
  9. 发布一个持续集成的npm包
  10. 如何防止PHP进程异常退出(进程被杀)?