
不过呢,我现在还是一学生,如有高见请指教!

listbox
一.根据需要自己定义item的颜色
1.首先要设置其DrawMode属性,设置DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable (有大小可变的项时使用)
2.实现其DrawItem事件响应
我写的代码如下:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
e.DrawBackground();
            Brush myBrush = Brushes.Red;
            Brush otherBrush=Brushes.Black;
            if (e.Index == 2)
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
            else
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, otherBrush, e.Bounds, StringFormat.GenericDefault);
        }
注:e.DrawBackground(); 一定要写哦,否则你都不知道自己选择了哪个的!
dataGridView
一.设置行颜色
1、首先设置selectionMode为FullRowSelect
2、设置AllowUserToAddRows属性为false(否则会发生索引错误的)
下面就是我写的代码了(省略了具体的应用设置,通过对.rows[e.RowIndex]的判断就可以了):
DataGridViewCellStyle style = new DataGridViewCellStyle();
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
          style.BackColor = Color.Red;
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle = style;
}
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号