C# — WinForm DataGridView篇
2015-12-18 19:45
543 查看
C# — WinForm DataGridView篇
最近用C#的winform开发了一个项目,从中积累了一些各个控件的使用方法,特将部分方法共享。
这一篇的主角是DataGridView:
属性组如下:
1、背景颜色设置
2、是否一次性支持选择多条记录
3、选择模式,一点击就是选择一行
4、绑定数据源后的隐藏列属性
5、绑定数据源后的冻结列属性,使用户在使用横向条查看数据时,也能时刻看到是谁的数据,提高用户体验
冻结了第一列的数据
方法组如下:
1、DataGridView控件的标题列设置序号,同时设置默认宽度
效果如图1所示:
2、datagirdview的行双击事件
三种点击方法的比较:
三种方法,我个人都用过,最后还是由衷地觉得CellDoubleClick方法是比较符合我现在的这个需求的:
CellClick方法是当用户一点击,就执行事件,有时用户不经意的点击都会触发执行事件;
CellContentClick方法是当用户点击DataGirdView单元格中的内容时,才会触发该事件,用户在使用时,有时他不能准确地点击到单元格内容,导致执行效果老是出不来;
CellDoubleClick方法是当用户双击某一行的数据时,执行事件,配合选择模式(SelectionMode)属性,效果不错。
3、DataGirdView中的数据转化为DataTable
4、DataGirdView中的数据导出到Excel,其中调用了上次转化成DataTable的方法,用的是网上导出到Execl的方法,但同时增加了对数字与时间字段的判断
分类: C#
好文要顶 关注我 收藏该文
guolebin7
关注 - 22
粉丝 - 37
+加关注
3
0
(请您对文章做出评价)
« 上一篇:毕设之简洁版毕业论文
» 下一篇:C#
— WinForm TreeView篇
最近用C#的winform开发了一个项目,从中积累了一些各个控件的使用方法,特将部分方法共享。
这一篇的主角是DataGridView:
属性组如下:
1、背景颜色设置
2、是否一次性支持选择多条记录
3、选择模式,一点击就是选择一行
4、绑定数据源后的隐藏列属性
this.dataGridView.Columns["emp_photo"].Visible = false;
5、绑定数据源后的冻结列属性,使用户在使用横向条查看数据时,也能时刻看到是谁的数据,提高用户体验
冻结了第一列的数据
this.dataGridView.Columns[0].Frozen = true;
方法组如下:
1、DataGridView控件的标题列设置序号,同时设置默认宽度
private void dView_employee_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { dView_employee.RowHeadersWidth = 60; for (int i = 0; i < dView_employee.Rows.Count; i++) { int j = i + 1; dView_employee.Rows[i].HeaderCell.Value = j.ToString(); } }
效果如图1所示:
2、datagirdview的行双击事件
private void dView_employee_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { /*判断点击行标题与列标题*/ if (e.RowIndex > -1 && e.ColumnIndex > -1) { /*传员工工号过去*/ emp_id = dView_employee.CurrentRow.Cells[0].Value.ToString(); Employee_file file = new Employee_fil(); file.Show() //this.Hide(); } }
三种点击方法的比较:
三种方法,我个人都用过,最后还是由衷地觉得CellDoubleClick方法是比较符合我现在的这个需求的:
CellClick方法是当用户一点击,就执行事件,有时用户不经意的点击都会触发执行事件;
CellContentClick方法是当用户点击DataGirdView单元格中的内容时,才会触发该事件,用户在使用时,有时他不能准确地点击到单元格内容,导致执行效果老是出不来;
CellDoubleClick方法是当用户双击某一行的数据时,执行事件,配合选择模式(SelectionMode)属性,效果不错。
3、DataGirdView中的数据转化为DataTable
public DataTable GetDgvToTable(DataGridView dgv) { DataTable dt = new DataTable(); for (int count = 0; count < dgv.Columns.Count; count++) { DataColumn dc = new DataColumn(dgv.Columns[count].Name); dt.Columns.Add(dc); } for (int count = 0; count < dgv.Rows.Count; count++) { DataRow dr = dt.NewRow(); for (int countsub = 0; countsub < dgv.Columns.Count; countsub++) { dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value); } dt.Rows.Add(dr); } return dt; }
4、DataGirdView中的数据导出到Excel,其中调用了上次转化成DataTable的方法,用的是网上导出到Execl的方法,但同时增加了对数字与时间字段的判断
public void ExportTOExcel(DataGridView dView) { /*用的三层结构,调用了一个类*/ using (BLLConnect a = new BLLConnect()) { DataTable myDt = GetDgvToTable(dView); SaveFileDialog saveFileDialog = new SaveFileDialog(); if (myDt.Rows.Count == 0) { MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { saveFileDialog.Filter = "Execl文件|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.Title = "导出文件保存路径"; saveFileDialog.FileName = null; saveFileDialog.ShowDialog(); string FileName = saveFileDialog.FileName; if (FileName.Length != 0) { DataTable dt = myDt; FileStream objFileStream; StreamWriter objStreamWriter; string strLine = ""; objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode); for (int i = 0; i < dt.Columns.Count; i++) { strLine = strLine + dt.Columns[i].ColumnName.ToString() + Convert.ToChar(9); } objStreamWriter.WriteLine(strLine); strLine = ""; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { if (j == 0 || j == 7 || j == 15 || j == 17) /*数字*/ { strLine = strLine + "@" + Convert.ToString(dt.Rows[i][j]) + Convert.ToChar(9); } else if (j == 10 || j == 13) /*时间*/ { strLine = strLine + string.Format("{0:d}", Convert.ToDateTime(dt.Rows[i][j])) + Convert.ToChar(9); } else { strLine = strLine + Convert.ToString(dt.Rows[i][j]) + Convert.ToChar(9); } } objStreamWriter.WriteLine(strLine); strLine = ""; } objStreamWriter.Close(); objFileStream.Close(); MessageBox.Show("数据导出成功","提示"); } } } }
分类: C#
好文要顶 关注我 收藏该文
guolebin7
关注 - 22
粉丝 - 37
+加关注
3
0
(请您对文章做出评价)
« 上一篇:毕设之简洁版毕业论文
» 下一篇:C#
— WinForm TreeView篇
相关文章推荐
- Web窗体开发
- 关于C#结构体和byte[]之间的转换
- C# 尝试读取或写入受保护的内存,这通常指示其他内存已损坏。错误类型为:System.AccessViolationException。
- C#随机函数使用方法
- c# 中的 for vs foreach
- C#中常用的ToString()方法总结
- C#获取Honeywell voyager 1400g扫码后的数据
- C#当中的多线程_任务并行库(中)
- C#聊天,C#简易聊天室
- c#中的多线程异常
- C#之值类型与引用类型
- C# 中自定义Attribute值的获取与优化
- C#验证字符串是否是数字,是否包括中文,是否是邮箱格式,是否是电话格式
- C#文件上传头文件
- NET 2.0(C#)调用ffmpeg处理视频的方法(转载)
- C#遍历文件夹下所有文件
- 分分钟用上C#中的委托和事件
- C#多线程那点事——信号量(Semaphore)
- C# 实现繁体字和简体字之间的转换
- 谈C#中的Delegate