我有一个遍历gridview的循环,并计算性别字段设置为' M'单击按钮时。 我也想用相同的条件改变那一行的那种颜色,即如果性别是' M'。
到目前为止,这是我的循环,非常感谢任何帮助!
protected void Button2_Click(object sender, EventArgs e)
{
int intfemdelegates = 0;
foreach(GridViewRow oItem in GridView1.Rows)
{
if (oItem.Cells[6].Text.Contains('F'))
{
intfemdelegates = intfemdelegates + 1;
GridView1.RowStyle.BackColor = System.Drawing.Color.Red;
}
Label2.Text = Convert.ToString(intfemdelegates);
}
}
答案 0 :(得分:0)
此代码可能有用......祝你好运!
int intfemdelegates = 0;
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if (dataGridView1[1, i].Value.ToString() == "F")
{
intfemdelegates = intfemdelegates + 1;
dataGridView1[1, i].Style.BackColor = Color.Red;
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
}
}
lblcount.Text = intfemdelegates.ToString();
答案 1 :(得分:0)
您正在整个Grid1上更改RowColor。改为这个
protected void Button2_Click(object sender, EventArgs e) {
int intfemdelegates = 0;
foreach(GridViewRow oItem in GridView1.Rows) {
if (oItem.Cells[6].Text.Contains('F')) {
intfemdelegates = intfemdelegates + 1;
oItem.BackColor = System.Drawing.Color.Red;
}
Label2.Text = Convert.ToString(intfemdelegates);
}