[英]changing gridview row color on condition within button click function
我有一個循環遍歷我的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);
}
}
此代碼可能有用...祝您好運!
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();
您正在整個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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.