iT邦幫忙

0

問一下DataGridView顯示兩種顏色

小魚 2018-01-21 19:25:1312000 瀏覽

問一下WinForm的DataGridView,
我知道可以每一格顯示不同的顏色,
但現在有一個需求,
希望一格裡面的上面一行跟下面一行,
顯示不同的顏色,
不知道這個功能有辦法做到嗎?
麻煩大大幫忙回答一下了,
謝謝~
/images/emoticon/emoticon41.gif/images/emoticon/emoticon41.gif

untitled iT邦新手 5 級 ‧ 2018-01-22 09:33:57 檢舉
可以指定到row就好 ,不需要到cells
for x = 0 to DataGridView1.rows.count-1
if index %2==0 then
DataGridView1.Rows(x).DefaultCellStyle.BackColor = System.Drawing.Color.Red
else
DataGridView1.Rows(x).DefaultCellStyle.BackColor = System.Drawing.Color.Blue
end if
next
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
石頭
iT邦高手 1 級 ‧ 2018-01-21 20:08:40


private void dataGridView1_DataBindingComplete
(object sender, DataGridViewBindingCompleteEventArgs e)
{
    int index = 0;
    int spliteCell = 3;//每三行換一個顏色

    foreach (DataGridViewRow dgv in dataGridView1.Rows)
    {
        if(index % spliteCell == 0)
            row.DefaultCellStyle.BackColor = Color.Red; //改row的顏色
        else if(index % spliteCell == 1)
            row.DefaultCellStyle.BackColor = Color.Blue; //改row的顏色
        else if(index % spliteCell == 2)
            row.DefaultCellStyle.BackColor = Color.Yellow; //改row的顏色
            
        index++;
    }
}

你的需求是這樣嗎?

小魚 iT邦大師 1 級 ‧ 2018-01-22 11:57:14 檢舉

不是,
我的需求是一個Cell可能上下兩行會有兩種狀態,
兩行的顏色要不一樣,
在同一個Cell裡面,
有辦法做到嗎?

0
froce
iT邦大師 1 級 ‧ 2018-01-22 11:04:51

不能去找到 DataGridView 的id,然後用CSS解決嗎?
https://www.w3schools.com/cssref/sel_nth-child.asp

小魚 iT邦大師 1 級 ‧ 2018-01-22 11:57:46 檢舉

CSS ?
你知道DataGridView是單機的嗎 ??

froce iT邦大師 1 級 ‧ 2018-01-22 12:10:15 檢舉

我看成webform...Orz

我要發表回答

立即登入回答