WinForm DataGridView控件隔行变色的小例子

复制代码 代码如下:

 dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;

隔行变色

复制代码 代码如下:

///
         /// 隔行变色
         ///

         /// 传入DataGridView控件名称
         public static void DgvRowColor(System.Windows.Forms.DataGridView dgv)
         {

             if (dgv.Rows.Count != 0)
             {
                 for (int i = 0; i < dgv.Rows.Count; i++)
                 {
                     if ((i + 1) % 2 == 0)
                     {
                         dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                     }
                     else
                     {
                         dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(224, 254, 254);
                     }                 
                 }
             }
         }

你可能感兴趣的:(WinForm DataGridView控件隔行变色的小例子)