我想让datagridview中某一行被选中时,textbox中显示选中的值,datagridview的选中模式是整行:
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
然后
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
int index = dataGridView1.CurrentRow.Index; //获取选中行的行号
textBox1.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();
}
运行时选择datagridview里面的行没有问题,但是当我不小心点击到datagridview的列头时,就提示如下错误:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
这个怎么办?正常情况下点击列头就应该是没有反应的

C#中datagridview选中行后textbox显示选中的内容
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (this.dataGridView1.SelectionMode != DataGridViewSelectionMode.FullColumnSelect)
{
int index = dataGridView1.CurrentRow.Index; //获取选中行的行号
textBox1.Text = dataGridView1.Rows[index].Cells[].Value.ToString();
};
}

C#中datagridview选中行后textbox显示选中的内容的更多相关文章

  1. C#——DataGridView选中行,在TextBox中显示选中行的内容

    C#--DataGridView选中行,在TextBox中显示选中行的内容,在DataGridView的SelectionChanged实践中设置如下代码 private void dataGridV ...

  2. vue2.0 element-ui中的el-select选择器无法显示选中的内容

    我使用的是element-ui V2.2.3.代码如下,当我选择值得时候,el-select选择器无法显示选中的内容,但是能触发change方法,并且能输出选择的值. select.vue文件 < ...

  3. 删除DataGridView选中行并更新数据库

    前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会常常出现,也非常有用.通过DataGridView对数据库进行更改和查询都比較方便. 这里我们须要 ...

  4. Ext.Net 使用总结之GridPanel中的选中行

    1.判断GridPanel中是否选中了某行 if (!GridPanel1.hasSelection()) { Ext.Msg.alert("提示", "请选择记录!&q ...

  5. c# winform 中DataGridView绑定List<T> 不能显示数据

    遇到问题 DataGridView绑定List后,List更新后再次绑定不显示数据 datagridview 绑定数据源的时候 用List是不能显示修改内容的..要用binginglist<T& ...

  6. 关于在elasticSearch中使用聚合查询后只显示10个bucket的问题

    先看下面es查询语句 { "size": 0, "aggs" : { "all_articleId" : { "terms&quo ...

  7. DataGridView 选中行 分类: DataGridView 2015-01-22 09:07 51人阅读 评论(0) 收藏

    说明: (1)命名 DataGridView 名称:dgvStockFirst 行索引:recordIndex (2)设置DataGridView属性: SelectionMode=FullRowSe ...

  8. Delphi DBGridEH中,选中行、列、单元格

    // 新增行后,默认首列 procedure TForm1.ADOQuery1AfterInsert(DataSet: TDataSet);begin  with DBGridEh1 do  begi ...

  9. 获取table中CheckBox选中行的id

    方式一 var selectList=''; jQuery(".table tbody input[type=checkbox]:checked").map(function () ...

随机推荐

  1. 又见链表 --- 另一种Creat方式与反转

    链表 作为一种数据结构,链表以其方便的增删查改功能,实现了无数经典有用的程序. 在之前的帖子里,我构建链表的方式是建立一个不储存数据的head节点,然后通过一边输入数据一边建立结点的方式构建整个链表. ...

  2. HDU 1686 Oulippo

    http://acm.hdu.edu.cn/showproblem.php?pid=1686 题意:给定一个文本串和给定一个模式串,求文本串中有几个模式串. 思路:直接套用KMP模板. #includ ...

  3. hdu 1370 || poj 1006 简单的中国剩余定理或者暴力

    Biorhythms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Probl ...

  4. Ubuntu 16.04 kinetic 编译指定包

    编译指定包 catkin_make -DCATKIN_WHITELIST_PACKAGES=baoming 使用上述命令后catkin_make会一直编译上面那个包,想要编译全部包,使用 catkin ...

  5. Jmeter 中Cookie管理器的使用

    Jmeter中有好几个地方可以管理Cookie,比如 Http Cookie Manager HTTP Header Manager jmeter.property文件中: #CookieManage ...

  6. [粒子特效]osg的自带粒子系统osgParticle::ParticleSystem

    osgParticle示例简单的演示了在osg中使用粒子系统的效果,用到了osgParticle库中的相关类,在osgParticle中主要有: (以下部分材料摘取自osg向场景中添加osgParti ...

  7. [调试][程序打印]当printf不能用时,使用C++的不定参数来搞定OutputDebugString打印

    void _trace( char* str , ...) //类似printf{ va_list vlist; va_start(vlist , str ); char a [100] ; vspr ...

  8. Java 常用对象-String类

    2017-11-02 20:02:06 String:代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能 ...

  9. SQLServer创建用户、数据库、表、约束、存储过程、视图

    --创建登录账户和数据库用户 ' exec sp_grantdbaccess 'sysAdmin','aa' --给数据库用户赋权限 grant select,update,insert,delete ...

  10. Windows Update 自动更新 设定 被锁(变灰)

    估计是McAfee自动更改掉的. 真TM烦人. 方法 1 不过找到了回复方法了: http://www.askvg.com/how-to-change-windows-update-settings- ...