首页 新闻 会员 周边

C#DataGrid根据某列值得到该行

0
[已解决问题] 解决于 2013-04-28 14:02

情况是:DataGrid控件里面现在有三条数据,有ID,Name,Sex,FSN四列。现在我要得到FSN的值为“111”的行,并让该行高亮显示,请问要怎么做啊。不是DataGridView,也不是选中行。

小小程序媛的主页 小小程序媛 | 菜鸟二级 | 园豆:202
提问于:2013-04-16 14:14
< >
分享
最佳答案
0
System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();
            grid.ItemDataBound += grid_ItemDataBound;

void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if ((e.Item.Cells[0].Text == "111"))
            {
                e.Item.Attributes.Add("style", "background:red");
            }
        }

好好学习MVC吧,用控件害死人。

奖励园豆:5
写代码的小2B | 老鸟四级 |园豆:4371 | 2013-04-16 14:48

谢谢!可是我们公司都是是做winform项目的啊。

小小程序媛 | 园豆:202 (菜鸟二级) | 2013-04-16 14:50

兄弟啊,你这个方法我的项目不能用啊,还有别的办法么。

小小程序媛 | 园豆:202 (菜鸟二级) | 2013-04-16 14:56
System.Windows.Forms.DataGridView dgv = new System.Windows.Forms.DataGridView();
            foreach (System.Windows.Forms.DataGridViewRow gvr in dgv.Rows)
            {
                if (gvr.Cells["FSN"].Value.ToString() == "111")
                {
                    System.Windows.Forms.DataGridViewCellStyle cellStyle = new System.Windows.Forms.DataGridViewCellStyle();
                    cellStyle.BackColor = System.Drawing.SystemColors.ButtonShadow;
                    gvr.DefaultCellStyle = cellStyle;
                }
            }

你感受一下。

写代码的小2B | 园豆:4371 (老鸟四级) | 2013-04-16 15:16

谢谢你的热心解答,你的这个方法是针对于DataGridView的,我的是DataGrid.所以你这个方法还是用不起。但是也学到了。把分给你吧。

小小程序媛 | 园豆:202 (菜鸟二级) | 2013-04-28 14:02

@小小程序媛: 我也碰到同样的问题 你是怎么解决的呀 求教育

Flying_Boy | 园豆:185 (初学一级) | 2013-05-30 11:04

@小小程序媛: 什么时候能给回复啊

Flying_Boy | 园豆:185 (初学一级) | 2013-05-30 14:21
其他回答(1)
0

这个应该很简单的

不负春光,努力生长 | 园豆:1382 (小虾三级) | 2013-04-18 13:24

是挺简单的,当时没想到。

支持(0) 反对(0) 小小程序媛 | 园豆:202 (菜鸟二级) | 2013-04-28 14:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册