第一列为自动序号,可在绑定的datatable中添加一列,把序号加进去.
第二列颜 可以用下面代码实现
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.FromName(dataGridView1.Rows[i].Cells[1].Value.ToString());
}
------------------------------------------------------------------------------------------
datagridview在绑定的前提下,怎样设置第一列自动添加序号,第二列单元格颜为数据的对应的颜
可能没说清楚,数据源里的第二列的值为red,blue等字符,datagridview的第二列的单元格的颜则为相应的颜,而不是显示red,blue等字符
----------------------------------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
this.dataGridView1.DataSource = Data.Northwind.GetProducts();
this.BuildColor();
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
datagridview数据源// 行号
SolidBrush B = new SolidBrush(Color.Black);
e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, B, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
}
/// <summary>
/// 自定单元格颜
/// </summary>
private void BuildColor()
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1[0, i] != null && this.dataGridView1[0, i].Value != null)
{
// 根据条件设置不同的颜
if (this.dataGridView1[0, i].Value.ToString() == "1")
this.dataGridView1[0, i].Style.BackColor = Color.Blue;
else if (this.dataGridView1[0, i].Value.ToString() == "10")
this.dataGridView1[0, i].Style.BackColor = Color.Yellow;
}
}
}
--------------------------------------------------------------------------------------------------------
/// <summary>
/// 自定单元格颜
/
// </summary>
private void BuildColor()
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1[0, i] != null && this.dataGridView1[0, i].Value != null)
{
// 根据条件设置不同的颜
if (this.dataGridView1[0, i].Value.ToString() == "1")
{
// 背景和前景都设成一样就达到你要的效果了
this.dataGridView1[0, i].Style.BackColor = Colo
r.Blue;
this.dataGridView1[0, i].Style.ForeColor = Color.Blue;
}
else if (this.dataGridView1[0, i].Value.ToString() == "10")
{
this.dataGridView1[0, i].Style.BackColor = Color.Yellow;
this.dataGridView1[0, i].Style.ForeColor = Color.Yellow;
}
}
}
}
-------------------------------------------------------------------------------------------------------