关于C#中dataGridView常用的功能(一)
来源:互联网 发布:小儿常见病推拿软件 编辑:程序博客网 时间:2024/04/25 07:46
C#中dataGridView的常用功能
使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据,将数据绑定到 dataGridView 控件非常简单和直观,在大多数情况下,只需设置 DataSource 属性即可。dataGridView 控件支持标准 Windows 窗体数据绑定模型,与BindingSource组件结合,从而能够在窗体中显示来自某一个数据源中的所有数据。dataGridView通常绑定到 BindingSource 组件,并将 BindingSource 组件绑定到其他数据源或使用业务对象填充该组件。BindingSource 组件为首选数据源,因为该组件可以绑定到各种数据源,并可以自动解决许多数据绑定的问题,有关BindingSource绑定数据的更多信息可查看相关的资料。
在这里,我想通过介绍dataGridView与bindingSource一同绑定数据源的数据,并将数据源中的数据显示在窗体中。之后,介绍一些关于dataGridView中的操作,一边自己或者其他读者用到时方便查找。当然,很明显的是,在这里,我不想过多的介绍dataGridView控件的用法,因为实在是很多,如果是想了解更多,可看这个链接点击打开链接
(1)首先是通过bindingSource组件来与数据源,在这里以Access2003数据库为例,进行绑定,以下是具体的实现步骤截图。然后添加dataGridView控件,设置dataGridView控件的DataSource属性为Access数据源中的某一个表,点击确定之后,即成功进行了绑定,运行之后即可看到表StudentInfo的信息,以下是详细步骤截图
(2)搞定了DataGridView显示数据源中的数据,就下来就实现DataGridView上,我经常需要用到的用法,在这里对上面的窗体进行些许美观的改进,然后会将源代码上传至此处,可供下载,查看(注意,在这里是采用VS2008,Access2003数据库)
首先,是设置dataGridView中的SelectionMode属性
先看看现在的运行效果
然后,来看看双击某一行,获取此行中所有单元格的信息,部分代码如下,需要设置dataGridView中的双击单元格事件对应的函数
该函数对应的代码如下:
private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { //MessageBox.Show("Yes"); //通过在dataGridView中设定 SelectionMode属性为FullRow,即可实现在遍历dataGridView时是一行一行的 //获取了DataGridView所在行的所有单元格的内容,并写入另外一个窗体中 //要求:各个textBox均为Public,即通过设置每一个textBox的Modifier为public才可以访问 string strNo = this.dataGridView.CurrentRow.Cells[0].Value.ToString(); string strName = this.dataGridView.CurrentRow.Cells[1].Value.ToString(); string strAge = this.dataGridView.CurrentRow.Cells[2].Value.ToString(); string strClass = this.dataGridView.CurrentRow.Cells[3].Value.ToString(); string strMath = this.dataGridView.CurrentRow.Cells[4].Value.ToString(); string strEnglish = this.dataGridView.CurrentRow.Cells[5].Value.ToString(); string strChinese = this.dataGridView.CurrentRow.Cells[6].Value.ToString(); Student stu = new Student(); stu.textBoxOne.Text = "学生的学号: " + strNo; stu.textBoxTwo.Text = "学生的姓名: " + strName; stu.textBoxThree.Text = "学生的年龄: " + strAge; stu.textBoxFour.Text = "学生所在班级: " + strClass; stu.textBoxFive.Text = "学生数学成绩: " + strMath; stu.textBoxSix.Text = "学生英语成绩: " + strEnglish; stu.textBoxSeven.Text = "学生语文成绩: " + strChinese; stu.textBoxOne.Visible = true; stu.textBoxTwo.Visible = true; stu.textBoxThree.Visible = true; stu.textBoxFour.Visible = true; stu.textBoxFive.Visible = true; stu.textBoxSix.Visible = true; stu.textBoxSeven.Visible = true; stu.Text = "学生: " + strName + " 的个人信息"; stu.ShowDialog(); }运行效果如图
这是我个人做项目经常用到的,另外,在上传的原代码中,也包含了一些其他的有趣的东西,如根据登陆的管理员级别,可以设置那些内容(其实是控件的visible的true或者false)可见,哪些内容又不可见。这在登陆窗体中,根据登陆的用户名检索数据库表中该用户名对应的管理员级别来确定那些窗体是属于他可见的。最后,是在实现将某一个当前的单元格的内容cells[i].value.toString()传至另外的窗体中的某一个控件进行显示时,要注意该控件需要设置Modifier属性为Public。代码下载链接点击打开链接
如图所示
0 0
- 关于C#中dataGridView常用的功能(一)
- C# dataGridView常用功能
- C# WinForm中DataGridView的修改删除功能
- C#中对DatagridView的部分常用操作
- C#中对DatagridView的部分常用操作
- C#中对DatagridView的部分常用操作
- C#中对DatagridView的部分常用操作
- C#中对DatagridView的部分常用操作
- C#中对DatagridView的部分常用操作
- C# winform中DataGridView最常用的知识点
- 关于datagridview的常用操作
- C#中对DatagridView部分常用操作
- C#中对DatagridView部分常用操作
- C#中对DatagridView部分常用操作
- C#中对DatagridView部分常用操作
- C#中对DatagridView部分常用操作
- C#中对DatagridView部分常用操作
- C#中对DatagridView部分常用操作
- Java中HashMap实现原理浅析
- Linux下/proc目录简介
- MySQL运行状态show status中文详解
- 如何查询出ORACLE表的列的值是中文的
- Unity 3D-- 摄像机Clear Flags和Culling Mask属性用途详解
- 关于C#中dataGridView常用的功能(一)
- loadrunner 测试下载文件的测试脚本
- Android之ListView列表视图和界面跳转实现
- 有一个定期写博客的习惯,必将使程序员收益终身
- Centos6.2安装Memcached详细教程
- ubuntu中eclipse 不支持gbk编码问题解决办法
- 封装的DBUtil 简化增删改查
- UVA - 10313 Pay the Price 整数的划分问题 01背包
- Tomat集群 + Tomcat集群session共享 + Nginx负载 + Nginx静态资源代理