当前位置:编程学习 > C#/ASP.NET >>

DataGridView 设置行选中的问题~在线等

DataGridView绑定数据源后,显示正常。已知一批id。若DataGridView中的id与已知id相符,将DataGridView中的Row设置为选中状态,不知怎么做?
请高手指教一下。 --------------------编程问答-------------------- 加一列CheckBox,设置CheckBox为true选中状态.

或者

用Rows[i].Select()这个方法,让选中的反白显示.多个就是多条反白了,不过,还是CheckBox好看一些 --------------------编程问答-------------------- 选择dataGridView1的某个selectionMode(忘了是哪个模式了),然后dataGridView1.Rows.Add(row); --------------------编程问答--------------------         //一批id
List<string> ids = new List<string>();
        ids.Add("1");
        ids.Add("3");
        ids.Add("5");
        ids.Add("7");
        ids.Add("9");

//一个dgv

DataGridView dgv = new DataGridView();
...//准备好一个dgv 只是声明了。 没有详细添加。把你自己的弄进去

//
for (int i = 0; i <  dgv.Rows.Count; i++)
{
foreach (string var in ids)
                {
                    if(dgv.Rows[i].Cells["dgv中放id的列名"].Value.ToString()==var)
                        //此处放置你的方法。CheckBox或者Select由你了。
                }
}
--------------------编程问答-------------------- 如果选中的话,该行的背景色就变成BLUE,否则不变. --------------------编程问答-------------------- int position; // the selected row index. 
      position = 0;  // here, you set it to 0, the first record.

      // 通过ID找到记录的位置,设置position。
      dataGridView1.DataSource = tb;
      dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      BindingContext c = dataGridView1.BindingContext;
      if(c.Contains(tb))
      {
        BindingManagerBase b = c[tb];
        b.Position = position;
      }
--------------------编程问答--------------------             DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn();//定义一个datagirdview的复选框
            checkBox.HeaderText = "选择";
            dataGridView1.Columns.Insert(0, checkBox);//插入到第一列
            foreach (DataGridViewColumn c in dataGridView1.Columns)
                if (!(c.Index == 0))
                    c.ReadOnly = true;//除第一列外设为只读,这个看你自己需要设置 --------------------编程问答-------------------- dataGridView1.Rows[所选行号].Selected = true; --------------------编程问答-------------------- dataGridView1.Rows[所选行号].Selected = true;
这个好像不起作用呀 --------------------编程问答-------------------- datagridview1.selectedcells[0].tag为选中的一行的ID,在属性SELECTMODE 中选 fullrowselected --------------------编程问答--------------------

        
--------------------编程问答--------------------

            公司简介|招贤纳士|广告服务|银行汇款帐号|联系方式|版权声明|法律顾问|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
江苏乐知网络技术有限公司 提供商务支持
 Email:webmaster@csdn.net
Copyright © 1999-2010, CSDN.NET, All Rights Reserved
--------------------编程问答-------------------- 绑定数据事件(具体事件忘了,有段时间没用了   不知你说的是不是这个意思!)时判断id是否是已知的, 是则改变行背景色!和邮件的状态小图标做法差不多吧 ! --------------------编程问答-------------------- dataGridView1.Rows[所选行号].Selected = true --------------------编程问答-------------------- --------------------编程问答-------------------- 选择dataGridView1的属性selectionMode(FullRowSelect),dataGridView1.Rows.Add(row);
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,