一、根据列表数据改变行字体颜色或者背景色
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>();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为列的字段名fildNamename = 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 = GetColor(s.color);e.Appearance.ForeColor = color;//改变字体颜色//e.Appearance.BackColor = Color.Red;//改变背景色}}}}break;}}
5.效果预览如下:
点击添加名单–选择颜色等
显示列表如下,根据条件显示字体颜色
更多博客内容详见我的博客 Wang's Blog