DataGrid Background Color

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    #1

    DataGrid Background Color

    I have a windows forms application, currently with a datagrid, that I would like to dynamically change the background color on a row by row basis based off of values in child rows. Parent rows are customer based, and the child rows are order based. If order > x, then set background color to red. If order>y, then set background color to yellow. I would prefer to do all this when the datagrid is loaded, but I cannot find a way to get this to work. Any help would be greatly appreciated.

    Keep in mind that this is in regards to a windows forms app, and not asp or .aspx.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I have not found a nice way to do this myself, but I have gotten it to work by using the CellFormatting event.
    Unfortunatly it gets called for EVERY cell (as that cell becomes visible on the screen) and not on a row by row basis.

    Assume that I have a DataGridView called: dgvNeedMapping

    [code=c#]
    private void dgvNeedMapping_ CellFormatting( object sender, DataGridViewCel lFormattingEven tArgs e)
    {
    int rowidx = e.RowIndex;
    DataGridViewRow dgr = dgvNeedMapping. Rows[rowidx];
    Color startcolor = e.CellStyle.Bac kColor;//previous background color
    //now you can determine what color to use and apply it.
    //for me I did this:
    //dgr.DefaultCell Style.BackColor =Color.FromName (dgr.Cells["ColorName"].Value.ToString ());
    }
    [/code]

    Comment

    • mcfly1204
      New Member
      • Jul 2007
      • 233

      #3
      Thank you for the response. However, I was hoping that I would be able to do this using a datagrid as opposed to the datagridview. The datagrid was easier to display the parent/child information in one grid. I want to check a value in the child row, and then change the background color in the parent row if the specified criteria is met.

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        I broke down and am now using two dgv's, one showing customers, and the other shows the customer's invoices when selected. This works fine, but next question is how can I incorporate changing the background color of the parent row based on a column in a child row? I do not know enough about bound dgv's and how parent/child interaction works.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If you have switched to DataGridViews, you can edit the code i showed above to change the backgrounds

          Comment

          • mcfly1204
            New Member
            • Jul 2007
            • 233

            #6
            Originally posted by Plater
            If you have switched to DataGridViews, you can edit the code i showed above to change the backgrounds
            Your original code was for datagridview, which does not translate to datagrids. What I was asking about is whether or not it is possible to change the parent row background color based on values in a corresponding child row.

            My thoughts are that this could become complex given there are x number of rows per parent row. My solution was to create a view to include the needed field from the child row in the parent row. This way your original code will work as needed for the parent dgv.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Right I saw that, and was stumped as you at changing DataGrid's colors.
              BUT then you came back and said you were using dgvs, which I took to mean DataGridViews, which then falls under the realm of my posted code.
              Are you still using DataGrid objects, just no longer nested?

              Comment

              • mcfly1204
                New Member
                • Jul 2007
                • 233

                #8
                Originally posted by Plater
                Right I saw that, and was stumped as you at changing DataGrid's colors.
                BUT then you came back and said you were using dgvs, which I took to mean DataGridViews, which then falls under the realm of my posted code.
                Are you still using DataGrid objects, just no longer nested?
                I was initially using datagrid's due to the ease in displaying nested rows. I switched to datagridviews after your initial post and after reading through a lot of forums posts with little promise. Currently, I am working with two datagridviews bound by a relation.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Ok then, so you can use my code to change the background color of the datagridviews you are using then?

                  Comment

                  • mcfly1204
                    New Member
                    • Jul 2007
                    • 233

                    #10
                    Yes, the code you originally posted works for changing the background color of a datagridview based on a particular column within that datagridview.

                    Comment

                    Working...