iT邦幫忙

0

dataGridView2.Rows[i]修改單行顏色問題

程式碼如下:
foreach (DataRow ds2Dr in ds2.Tables[0].Rows)
{
if (ds2Dr["NORMAL"].ToString().Equals("Y"))
{
DataGridViewRow row = dataGridView2.Rows[j];
row.DefaultCellStyle.ForeColor = Color.Red;
}
j++;
}
//gridview2是從foreach那個table去生成的 所以資料行應該是能對應的

j為我設定的變數,測試時候把j直接改成固定數值 3 也沒有變色成功

另測試是否改色指令出錯 直接下
dataGridView2.DefaultCellStyle.ForeColor= Color.Red;
整個gridview是有成功變成紅通通的

測試使用
dataGridView2.Rows[1].Cells[1].Style.ForeColor = Color.Red;
也是變色失敗
懇請各位大大指點一下或是告知有何需要注意沒有改到,還是跟foreach有關。

GridView生成完畢直接改顏色也失敗
dataGridView2.DataSource = ds3.Tables["result"];
dataGridView2.Rows[3].DefaultCellStyle.ForeColor = Color.Red;

補充:在我另一程式使用相同指令是可以變色成功的

優悠 iT邦新手 3 級 ‧ 2018-10-08 17:07:35 檢舉
我好久沒用GRIDVIEW,我記得能基雙數不同色。
優悠 iT邦新手 3 級 ‧ 2018-10-08 17:11:14 檢舉
給你參考:
https://blog.xuite.net/s925517/mylife/76542214-%E8%BD%89%E8%BC%89%3AGridView%E4%BD%BF%E7%94%A8%E5%A4%A7%E5%85%A8+(C%23)
關鍵字:GridView突出顯示某一單元格
JC iT邦新手 2 級 ‧ 2021-11-07 23:53:52 檢舉
單一儲存格換顏色, 我是這樣用的
this.dataGridView1.Rows[index2].Cells[17].Style.BackColor = Color.Aqua;
指定背景標準色
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
Homura
iT邦高手 1 級 ‧ 2018-10-08 17:38:10

你是在RowDataBound事件裡做的嗎?
像這樣

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.BackColor = System.Drawing.Color.AliceBlue;
    }
}

我比較喜歡寫一個CSS讓GridView載入就好

<style>
    .tableStyle > tbody > tr:nth-child(2n+1){
        background-color:green;
    }
    .tableStyle > tbody > tr:nth-child(2n){
        background-color:yellow;
    }
</style>
<asp:GridView ID="GridView1" runat="server" CssClass="tableStyle"></asp:GridView>

Curry iT邦新手 5 級 ‧ 2018-10-11 09:14:18 檢舉

我是在vs用c#寫喔 前輩這些要放在哪我不太清楚

Homura iT邦高手 1 級 ‧ 2018-10-11 09:20:14 檢舉

Tatum
Webform 只能用vs寫啊.....
上面是gridview的事件
下面是gridview套用css而已@@

我要發表回答

立即登入回答