您的位置:程序门 -> .net技术 -> c#



怎么在代码中设置datagridview中的某个单元格为选定状态?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎么在代码中设置datagridview中的某个单元格为选定状态?[已结贴,结贴人:zhangdora]
发表于:2007-04-04 09:46:44 楼主
在程序代码中产生一个值v1,v1是datagridview中一个单元格的值,但是目前并不知道这个值所在的行号(列号知道),怎样使包含v1的这个单元格处于选定状态?多谢了!回复有效一定结分。
发表于:2007-04-04 09:59:041楼 得分:0
up
发表于:2007-04-04 10:07:502楼 得分:0
加个checkbox
发表于:2007-04-04 10:16:193楼 得分:0
private   void   datagridview1_cellformatting(object   sender,   datagridviewcellformattingeventargs   e)
{
        if   (datagridview1.columns[e.columnindex].name.equals( "列名 "))
        {
                //   ensure   that   the   value   is   a   string.
                string   stringvalue   =   e.value   as   string;
                if   (stringvalue   ==   null)   return;

                //   set   the   cell   tooltip   to   the   text   value.
                datagridviewcell   cell   =   datagridview1[e.columnindex,   e.rowindex];
                cell.tooltiptext   =   stringvalue;

                //   replace   the   string   value   with   the   image   value.
                switch   (stringvalue.trim())
                {
                        case   "v1 ":
                                e.cellstyle.backcolor   =   color.yellow;
                                cell.selected   =   true;
                                break;
                }
        }
}
发表于:2007-04-04 10:23:244楼 得分:20
完整解决:
                ///   <summary>
                ///   设置单元格
                ///   </summary>
                ///   <param   name= "value "> 要选择单元格的值 </param>
                ///   <param   name= "colindex "> 列索引 </param>
                private   void   setcurrentcell(object   value,int   colindex)
                {
                        try
                        {
                                foreach   (datagridviewrow   dgr   in   datagridview1.rows)
                                {
                                        //判断单元格值
                                        if   (dgr.cells[colindex].value   ==   value)
                                        {
                                                //设置当前单元格
                                                datagridview1.currentcell   =   dgr.cells[colindex];
                                                //设置选中状态
                                                dgr.cells[colindex].selected   =   true;
                                        }
                                }
                        }
                        catch   (exception   ex)
                        {
                                messagebox.show(ex.message);
                        }
                }
发表于:2007-04-04 10:28:085楼 得分:0
for   (int   i   =   0;   i   <   this.gridview.rows.count;   i++)
                        {
                                if   (this.gridview.rows[i].cells[列号].value.tostring()   ==   "值 ")
                                {
                                        this.gridview.rows[i].defaultcellstyle.backcolor   =   color.gold;
          break; 
                                }
                        }
发表于:2007-04-29 08:05:356楼 得分:0
mark


快速检索

最新资讯
热门点击