Method to set datagridview row background color

Hi all,

I had a datagridview. May I know how to set the particular row color of a datagridview to yellow

Please help.

Thanks



Answer this question

Method to set datagridview row background color

  • Andrew Jones


    Check that the following lines are executing or not

    row.DefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.ControlLight);
    row.ReadOnly =
    true;

    Because it is working fine at my end, i think the problem lies in your conditions.





  • Ben Luna

    I still can't get this to work. Here is a sample of what I have. In VS2005 I don't see that a default form load is created, so I added this to my constructor:

    public ChildForm()

    {

    InitializeComponent();

    this.xmlDoc = new XmlDocument();

    this.xmlDataSet = new DataSet();

    this.frameRateDS = new DataSet();

    this.gridViewFrameRateDS = new DataSet();

    this.Load += new EventHandler(ChildForm_Load);

    }

    Then in ChildForm_Load I try setting the default row color and read only property as in the following but it does not work.

    private void ChildForm_Load(object sender, System.EventArgs e)

    {

    XmlNode frameRateSet = this.xmlDoc.SelectSingleNode("ROOT/ScanSimplex/ScanTissue/frameRates");

    if (frameRateSet == null)

    return;

    this.frameRateDS.ReadXml(new XmlTextReader(new StringReader(frameRateSet.OuterXml)));

    this.transposeDataSetForGridView(this.frameRateDS, this.gridViewFrameRateDS);

    frameRateGridView.DataSource = this.gridViewFrameRateDS;

    frameRateGridView.DataMember = "frameRateSet";

    frameRateGridView.Columns[0].Frozen = true;

    frameRateGridView.Columns[0].ReadOnly = true;

    frameRateGridView.Columns[0].DefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.ControlLight);

    foreach (DataGridViewColumn col in frameRateGridView.Columns)

    col.SortMode = DataGridViewColumnSortMode.NotSortable;

    string subTable;

    foreach (DataGridViewRow row in frameRateGridView.Rows)

    {

    subTable = (string)row.Cells[0].Value;

    for (int table = 1; table < frameRateDS.Tables.Count; table++)

    {

    if (frameRateDS.Tables[table].TableName.Equals(subTable))

    {

    row.DefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.ControlLight);

    row.ReadOnly = true;

    }

    }

    }

    }


  • Alex - IntraLAN

    Yes you can do this by using the following code snippet

    int RowIndex = 0;

    this.dataGridView1.Rows[ RowIndex ].DefaultCellStyle.BackColor = Color.Yellow;



  • preim

    Okay, I wrote another test program and I am able to change the row colors and readonly attribute in that program. Not sure what I am doing wrong in this one. It is a long story, but I'm trying to write an XML editor app for work that hides the tags, etc. and for one portion of our data the datagridview would work nice. However, just loading the xml into a dataset and using datagridview with our data results in a view with 40+ columns and around 5 rows. I wanted to transpose the data so I have 40+ rows and 5 columns. I managed to create a dataset that is transposed but when I put it into the datagridview it is displayed as I expect and I can change column colors, etc. but not rows, so I think my problem may be related to something I am not doing in the transpose.


  • Palcouk

    I keep learning more but have no idea yet how to solve my problem. If I put the code into my main form load the datagridview appears the way I want it, but when it is in a child form load it does not.

    The problem is that I need to create child forms based on a File->Open operation to load the datagridview, so I don't know how to address this issue. Anyone else know how to proceed


  • Erwin De Leon

    Form Load is not created by Default , when working on the Windows Application double click the form to create the Form_Load() Method



  • Amnesiasoft

    Then write the code in the Form Load or in DataGrid Load, but be careful with the out of range exception



  • Wolf5

    I'm not trying to highlight the row when it is selected, but just change the color of certain rows statically.
  • Trinh Ng.

    It appears it is not related to my dataset. My suspicion now is that it is related to the use of MDI and child forms. In my test app the gridview is loaded in the form_load(). When I do this in the form_load() of a child form in an MDI app it no longer works.

    Suggestions


  • Learning 2 Code

    I set a breakpoint in that condition and it is getting there as I expected. This is really confusing me. Can you post more of what your test condition does
  • Daniel Hern&amp;#225;n Pallarez

    Thanks, I will give that a try and report the results.
  • cspoon

    Thanks. I was able to to get the form_load() to be created as you said and move the code to that method but the rows I try to change the background color on are still not changing.
  • cloudycity

    I have been unable to get this to work. I can set the alternating row backcolor without trouble, and column backcolor, but when I try to set a specific cell or an entire row to a desired backcolor, I get the default white. Any hints

    I am using VS 2005 .NET 2.0 and C#


  • Joey Joey

    I have this code in my project that changes the cell's backcolor, hope it will help

    private void tblNewlyDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)

    {

    tblNewlyDataGridView[e.ColumnIndex, e.RowIndex].Style

    .SelectionBackColor = Color.Green;

    }

    private void tblNewlyDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)

    {

    tblNewlyDataGridView[e.ColumnIndex, e.RowIndex].Style

    .SelectionBackColor = Color.HotPink;

    }



  • Method to set datagridview row background color