========================================================================================
==重点需要掌握==
AutoSizeColumnsMode 设置为File 列填充效果
RowHeadersVisible 设置false 第一列不显示
SelectionModel 设置FullRowSelect 行被选中
AllowUserToResizeColumns 设置False DataGridView是否可以调整列的大小
AllowUserToResizeRows 设置False DataGridView是否可以调整行的大小:

绑定DataGridView控件的方法:
this.datagridview.DataSource=DBHelper.ExecuteTable("sql语句").DefaultView;

取DataGridView的值绑定到文本框
实现方式:在CellClick事件中实现如下代码
方法1
int rowIndex=this.dataGridView控件.CurrentCell.RowIndex;
this.文本框名.Text = this.dataGridView控件[0,rowIndex].Value.ToString();
this.文本框名.Text = this.dataGridView控件[1,rowIndex].Value.ToString();
========================================================================================

注意:下列代码中的i是定义成全局变量
///[第一行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i <= 0)
{
MessageBox.Show("已经是第一行了!",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[0].Selected = true;
}
///[下一行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i < this.dataGridView1.RowCount - 1)
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[++i].Selected = true;
}
else
{
MessageBox.Show("已经是最后一行了!",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
///[上一行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i <= 0)
{
MessageBox.Show("已经是第一行了!",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[--i].Selected = true;
}
///[最后行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i < this.dataGridView1.RowCount - 1)
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Selected = true;
}
else
{
MessageBox.Show("已经是最后一行了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

winform——绑定DataGridView的更多相关文章

  1. Winform知识汇总之多次绑定DataGridView的DataSource会报错 NullReferenceExcepti

    最近做了一个winform的项目,数据绑定在datagridview中,datagridview中的数据需要删除,分页,更新等之类的操作,所以就涉及到了datagridview的重新绑定问题,而且这些 ...

  2. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  3. WinForm程序用使用List对象绑定DataGridView数据源

    1. 在用List<T>对象绑定DataGridView数据源属性的时候,数据源的内容不会动态更新,如果List<T>对象集合中的数据发生变化,那么数据控件的数据源是不会得到更 ...

  4. 多次绑定DataGridView的DataSource会报错 NullReferenceExcepti

    最近做了一个winform的项目,数据绑定在datagridview中,datagridview中的数据需要删除,分页,更新等之类的操作,所以就涉及到了datagridview的重新绑定问题,而且这些 ...

  5. WinForm Control - DataGridView

    http://blog.csdn.net/fangxing80/article/details/1561011 .NET 2.0 - WinForm Control - DataGridView 编程 ...

  6. wpf的datagrid和winform的datagridview刷新

    DataGrid的数据源的加载需要大量IO操作,不可能等数据全部读取之后才显示到UI上.由于对WPF数据绑定不很熟悉,对ObserveCollection等内容没有太多时间去研究,只能用一些取巧的办法 ...

  7. winform中DataGridView实现分页功能

    WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载)   转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...

  8. 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现

    关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...

  9. winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色

    winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...

随机推荐

  1. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  2. .NET: 防止多个应用程序同时开

    用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...

  3. Android -- 自定义View小Demo,关于Rect绘制Android机器人(一)

    1,关于Rect和RectF类的区别以前一直没有去关注它,刚刚了解了一下才知道都是用来确定矩形的区域,不过Rect是int类型的坐标而RectF是float类型的坐标,所以说RectF要更加精确.现在 ...

  4. My sql 日期格式化

    DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) select DATE_FORMAT(NOW(),'%Y-%m-%d %H ...

  5. 夺命雷公狗---DEDECMS----24dedecms让网站头部分离

    我们这里来做一个让网站头部分离的工作,我们先看下index.htm的模版, 这里很明显就是用了一个div包围着而已,那么我们在模版目录下创建一个head.htm,如下所示: 然后我们再将刚才div里面 ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON DivImage2

    zw版[转发·台湾nvp系列Delphi例程]HALCON DivImage2 procedure TForm1.Button1Click(Sender: TObject);var op : HOpe ...

  7. 5 Best Automation Tools for Testing Android Applications

    Posted In | Automation Testing, Mobile Testing, Software Testing Tools   Nowadays automated tests ar ...

  8. ID3DXMesh接口 创建自己的立方体网格

    D3DXCreateMeshFVF 首先创建一个ID3DXMesh接口. ID3DXMesh接口的说明可以参数龙书. 这里主要是用代码来讲解: #define VERTEX_FVF (D3DFVF_X ...

  9. python 提取图片转为16 24BPP 的方法

    python 中处理图片用的是 pil ,在 linux  和 win 上都可以使用. centOS 5.x 上安装的方法是 yum install python-imaging 24BPP: imp ...

  10. Linux, Mac下Shell 数组 Array 的修理工

    我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当 ...