C#Gridview改变⾏字体颜⾊或背景⾊⼀、根据列表数据改变⾏字体颜⾊或者背景⾊
1.ALT+ENTER键打开属性列表,根据Name.Color.Name获取颜⾊字符串
string color = tePhone1.Color.Name;
2.将颜⾊字符串存⼊数据库,附上数据库Dao代码
namespace rdms.DB
{
public class StudentDao
{
private static ILog LOG = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static List<Student> GetAll()
{
return DBContext.ScodeSession.From<Student>().Select().ToList();
}
public static int Update(Student s)
{
// DBContext.ScodeSession.FromSql("UPDATE Student SET color = '"+s.color+ "' where s_id = " + s.s_id);
return DBContext.ScodeSession.Update(s);
}
public static Student getStudent(int id)
{
List<Student> list = DBContext.ScodeSession.FromSql("SELECT * FROM Student where s_id = " + id).ToList<Student>();
gridview不显示if (list != null && list.Count > 0)
{
return list[0];
}
else
{
return null;
}
}
public static int Add(Student s)
{
return DBContext.ScodeSession.Insert(s);
}
public static void Delete(Student s)
{
DBContext.ScodeSession.Delete<Student>(s);
}
}
}
3.在显⽰列表点击Run Designer,在事件列表中到RowStyle,给⼀个事件名称
4.具体实现代码如下
//颜⾊字符串转颜⾊对象
public static Color GetColor(string ColorStr)
{
int ARGBvalue = 0;
Color newColor = Color.FromName(ColorStr);
if ((newColor.A + newColor.R + newColor.G + newColor.B) == 0)
{
int.TryParse(ColorStr, System.Globalization.NumberStyles.HexNumber, null, out ARGBvalue);
newColor = Color.FromArgb(ARGBvalue);
}
return newColor;
}
private void gridView2_RowStyle_2(object sender, RowStyleEventArgs e)
{
List<Student> list = StudentDao.GetAll();
string name = "";
while(e.RowHandle >= 0) {
if (gridView2.GetRowCellValue(e.RowHandle, "NAME")!=null) { //NAME为列的字段名fildName name = gridView2.GetRowCellValue(e.RowHandle, "NAME").ToString();
}
if (list != null && list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
Student s = list[i];
if (name != "" )
{
if (name.Equals(s.name))
{
Color color = lor);
e.Appearance.ForeColor = color;//改变字体颜⾊
//e.Appearance.BackColor = Color.Red;//改变背景⾊
}
}
}
}
break;
}
}
5.效果预览如下:
点击添加名单–选择颜⾊等
显⽰列表如下,根据条件显⽰字体颜⾊
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论