C# 奇数/偶数 datagridview 行背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18311253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:53:05  来源:igfitidea点击:

Odd/Even datagridview rows background color

c#.netwinformsdatagridview

提问by Marek

I have datagridview and now I would like to change background color of its each row depending whether row number is even or odd.

我有 datagridview,现在我想根据行号是偶数还是奇数来更改其每一行的背景颜色。

I thought that there must be easier way to reach that. Then using for example this part of code and modify it so it would change the colours of dtg's row. If this snippet of code is one of the ways to do that, may someone help me to improve it so it wouldn't throw exception when index is out if rabge?

我认为必须有更简单的方法来实现这一目标。然后使用例如这部分代码并对其进行修改,以便更改 dtg 行的颜色。如果这段代码是这样做的方法之一,有人可以帮我改进它,这样它就不会在 rabge 的情况下在 index 出来时抛出异常吗?

public void bg_dtg()
    {
        try
        {

            for (int i = 0; i <= dataGridView1.Rows.Count ; i++)
            {
                if (IsOdd(i))
                {

                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(""+ex);
        }
    }

   public static bool IsOdd(int value)
   {
       return value % 2 != 0;
   }

Thank you for your time and answers.

感谢您的时间和答案。

采纳答案by Adil

You are getting exception because you are accessing row that is not present. GridView rows are zero based index, it means if you have ten rows in grid the index will be from 0 to 9 and you should iterate one less then the rows count. The i <= dataGridView1.Rows.Countwill give exception on last iteration because when count is 10 (total rows are ten) and dataGridView1.Rows[10] does not exists therefore exception is thrown.

您正在访问异常,因为您正在访问不存在的行。GridView 行是从零开始的index,这意味着如果网格中有 10 行,则索引将从 0 到 9,您应该比行少迭代 1 行count。该i <= dataGridView1.Rows.Count会就最后一次迭代例外,因为当计数为10(总排十)和dataGridView1.Rows [10]不存在,因此抛出异常。

Change<= in loop condition to <

循环条件中的 <=更改为 <

for (int i = 0; i <= dataGridView1.Rows.Count ; i++)

To

for (int i = 0; i < dataGridView1.Rows.Count ; i++)

You Should AlternatingRowsDefaultCellStyleproperty to set alternative row style to keep it simple and efficient.

您应该AlternatingRowsDefaultCellStyle属性来设置替代行样式以保持简单和高效。

回答by Colin Steel

There is a DataGridViewalternate row view style option in the forms designer. AlternatingRowsDefaultCellStylein the properties grid

DataGridView表单设计器中有一个备用行视图样式选项。AlternatingRowsDefaultCellStyle在属性网格中

回答by Rohit

You can use AlternatingRowsDefaultCellStyle

您可以使用 AlternatingRowsDefaultCellStyle

OR

或者

you can also do it manually

你也可以手动完成

   foreach (DataGridViewRow row in dataGridView1.Rows)

            if (row.Index % 2==0 )
            {
                row.DefaultCellStyle.BackColor = Color.Red;    
            }

回答by sarder kamruzzaman polash

you can try this code

你可以试试这个代码

 for (int i = 0; i < GridView1.Rows.Count; i++) {

     if (i % 2 == 0) {
       GridView1.Rows[i].Cells[0].Style.BackColor = System.Drawing.Color.Green;
       GridView1.Rows[i].Cells[1].Style.BackColor = System.Drawing.Color.Green;
     }
     else {
       GridView1.Rows[i].Cells[0].Style.BackColor = System.Drawing.Color.Red;
       GridView1.Rows[i].Cells[1].Style.BackColor = System.Drawing.Color.Red;
     }
}

回答by Jaffer

AlternatingRowStyle-BackColor = "#C5C5C5"

We can directly add the code in the ASP grid

我们可以直接在ASP网格中添加代码

<asp:GridView ID="Gridview1" runat="server"
                                 AlternatingRowStyle-BackColor = "#F3F3F3" 
                                AutoGenerateColumns="true">
</asp:GridView>