답변됨 Datagridview Column Backcolor

  • 2012년 3월 12일 월요일 오전 4:30
     
     

    I'm trying to change the backcolor of the cells in one column in a datagridview. I've got the following in the Form load event-     ComponetsDataGridView.Columns["Quantity"].DefaultCellStyle.BackColor = Color.Red;

     But it does not change the backcolor however if I use this- 

    ComponetsDataGridView.Columns["Quantity"].DefaultCellStyle.ForeColor = Color.Green;

    It works and changes the forecolor.


    why767

모든 응답

  • 2012년 3월 12일 월요일 오전 5:01
     
      코드 있음

    Sorry I cannot reproduct your issue, everything goes well with me very fine——

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object senderEventArgs e)
            {
                DataGridView dv = new DataGridView() { Parent = thisDock = DockStyle.Fill };
                dv.DataSource = new[] { new{Id=1,Quality=10}};
                dv.Columns["Quality"].DefaultCellStyle.BackColor = Color.Red;
            }
          
        }


       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

  • 2012년 3월 12일 월요일 오후 12:15
     
     
    I have the RowsDefaultCellStyle BackColor = ButtonFace and AlternatingRowsDefaultCellStyle BackColor = ControlLightLight in design mode. Would that override changing the settings in the form_load?

    why767

  • 2012년 3월 13일 화요일 오전 12:21
     
      코드 있음

    Hi, 

    No, Design time properties will be set when Initialization of controls happen.

    As Wei_Dong shows this should work as it is unless you have multiple points where you control your styles. If this is the case you should be checking by wiring CellStylechange event,  who are all influencing style change in cell

      dataGridView1.CellStyleContentChanged += dataGridView1_CellStyleContentChanged;
    Hope this helps you...

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

  • 2012년 3월 13일 화요일 오전 1:48
     
     답변됨 코드 있음
    I have the RowsDefaultCellStyle BackColor = ButtonFace and AlternatingRowsDefaultCellStyle BackColor = ControlLightLight in design mode. Would that override changing the settings in the form_load?

    Yes,this is really the problem!So you should:

    1)Cancel setting the two properties you've mentioned above。

    2)

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object senderEventArgs e)
            {
                DataGridView dv = new DataGridView() { Parent = thisDock = DockStyle.Fill };
                dv.DataSource = new[] { new{Id=1,Quality=10}};
                dv.RowsDefaultCellStyle.BackColor = SystemColors.ButtonFace;
                dv.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;

                foreach (DataGridViewRow item in dv.Rows)
                {
                    item.Cells["Quality"].Style.BackColor = Color.Red;
                }
            }
          
        }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    • 답변으로 표시됨 why767 2012년 3월 13일 화요일 오후 9:03
    •  
  • 2012년 3월 13일 화요일 오후 9:03
     
     

    The foreach looping through the rows works to set the
    backcolor. I don’t see why I can change the forecolor with DefaultCellStyle.ForeColor
    but not the backcolor.



    Thanks

    why767