111,129
社区成员
发帖
与我相关
我的任务
分享
/*
1:如何将第一列设为冻结。
2:判断单元格里的值为异常后 将该单元格背景颜色置为红色
3:绑定后如何调整 各列的列宽???
*/
public Form2()
{
InitializeComponent();
Bind();
dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
}
void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
for (int r = 0; r < dataGridView1.Rows.Count - 1; r++)
{
for (int c = 0; c < dataGridView1.Columns.Count; c++)
{
string str = dataGridView1[c, r].Value.ToString();
if (str == "异常") dataGridView1.Rows[r].Cells[c].Style.BackColor = Color.Red;
}
}
}
private void Bind()
{
DataTable dt = new DataTable();
dt.Columns.Add("编号", typeof(string));
dt.Columns.Add("状态1", typeof(string));
dt.Columns.Add("状态2", typeof(string));
dt.Columns.Add("状态3", typeof(string));
dt.Columns.Add("状态4", typeof(string));
AddValueToDataRow(dt, "2070204", "异常", "正常", "正常", "正常");
AddValueToDataRow(dt, "2080201", "正常", "正常", "正常", "正常");
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.Columns[0].Frozen = true;
}
private void AddValueToDataRow(DataTable dt, string c1, string c2, string c3, string c4, string c5)
{
DataRow addDR = dt.NewRow();
addDR[0] = c1;
addDR[1] = c2;
addDR[2] = c3;
addDR[3] = c4;
addDR[4] = c5;
dt.Rows.Add(addDR);
}