如何在datagridview中设置指定行的背景颜色? [英] How to set the background color for specified row in datagridview?

查看:509
本文介绍了如何在datagridview中设置指定行的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datagridview中设置指定Row的背景颜色。

我需要的是我有一个for循环(i = 0; i< 10; i ++)。在此为循环我写逻辑

as



if(i = 1)

{

//想要为此指定行设置颜色..

dataGridView1.SelectedRows [1] .DefaultCellStyle.SelectionBackColor = Color.Blue;

}



如果(i = 1)

{

//想要为此指定行设置颜色..

dataGridView1.SelectedRows [2] .DefaultCellStyle.SelectionBackColor = Color.Yellow;

}





if(i = 1)

{

//想要为此指定行设置颜色..

dataGridView1.SelectedRows [3] .DefaultCellStyle.SelectionBackColor = Color.Red;

}







但我没有得到预期的o / p。我希望你理解我的需要。请帮助我。提前感谢...

解决方案

你可能想要这个:

 dataGridView1。 SelectedRows [ 1 ]。DefaultCellStyle.BackColor = Color.Yellow; 

而不是SelectionBackColor,它仅在选择行时影响该行。


嘿!!我发现它来自Stack溢出...感谢您的所有顾虑..你去吧









dataGridView1.Rows [1] .DefaultCellStyle.ForeColor = Color.Red;







因为当用户只选择了行时,SelectedRows属性将返回行,如果没有选择行,则代码将抛出异常。


Hi, I want to set the background color for specified Row in datagridview ..
My need is i have a for loop (i=0;i<10;i++) .Inside this for loop i write the logic
as

if(i=1)
{
//Want to Set Color For This Specified Row..
dataGridView1.SelectedRows[1].DefaultCellStyle.SelectionBackColor = Color.Blue;
}

if(i=1)
{
//Want to Set Color For This Specified Row..
dataGridView1.SelectedRows[2].DefaultCellStyle.SelectionBackColor = Color.Yellow;
}


if(i=1)
{
//Want to Set Color For This Specified Row..
dataGridView1.SelectedRows[3].DefaultCellStyle.SelectionBackColor = Color.Red;
}



But i didn't get the expected o/p . I hope U understand My need . Please Help Me.Thanks in Advance ...

解决方案

You probably want this:

dataGridView1.SelectedRows[1].DefaultCellStyle.BackColor = Color.Yellow;

rather than SelectionBackColor which only affects the row when it is selected.


Hey !! I found it myself from Stack overflow ...Thanks for all your concerns ..Here u go




dataGridView1.Rows[1].DefaultCellStyle.ForeColor = Color.Red;



Because SelectedRows property will return rows when row(s) has been selected by the User only, if no rows are selected then your code will throw exception.


这篇关于如何在datagridview中设置指定行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆