c# highlight certain rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AaronW
    New Member
    • Nov 2007
    • 4

    #1

    c# highlight certain rows

    I am trying to do conditional formatting of rows in a datagrid and can not get DataGridItemEve ntArgs as an option. Is there something wrong with the assembly?

    using System;
    using System.Drawing;
    using System.Collecti ons;
    using System.Collecti ons.Specialized ;
    using System.Componen tModel;
    using System.Text.Reg ularExpressions ;
    using System.Threadin g;
    using System.Windows. Forms;
    using System.Data;
    using System.Timers;
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That is not much to go on.
    Are you sure you're not looking at descriptions for DataGridView and not DataGrid?

    Comment

    • AaronW
      New Member
      • Nov 2007
      • 4

      #3
      I am trying to loop through the datagrid and once I find a cell with certain information I would like to highlight the entire row.
      --------------------------------------------------------------
      private void BindUsers()
      {
      Domain = cboDomain.Selec tedValue.ToStri ng();
      OU = cboOU.SelectedV alue.ToString() ;
      SiteCode = cboSite.Selecte dValue.ToString ();

      DataSet ds = dsVendors(Domai n, OU, SiteCode);
      DataTable dt = ds.Tables["Vendors"];
      dgMain.DataSour ce = dt;
      dgMain.Alternat ingBackColor = Color.LightStee lBlue;

      // set the number of users
      intNumUsers = dt.Rows.Count;

      // finally compute our randomization
      ComputeRandomMe trics();
      }
      --------------------------------------------------------------

      I tried the following with the dataset but recieved sereval errors and figured I would be better off looping through the grid once the data is loaded.

      private void DataGridView1_C ellFormatting(o bject sender, System.Windows. Forms.DataGridV iewCellFormatti ngEventArgs e)
      {
      DataRowView drv;
      if (e.RowIndex >= 0) {
      if (e.RowIndex <= ds.Tables("Vend ors").Rows.Coun t - 1) {
      drv = ds.Tables("Vend ors").DefaultVi ew.Item(e.RowIn dex);
      Color c;
      if (drv.Item("loca tion").ToString == "ACTION REQUIRED") {
      c = Color.Red;
      }
      else {
      c = Color.Black;
      }
      e.CellStyle.Bac kColor = c;
      }
      }
      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        by higlight do you mean "select" or change the visible colors of?

        Comment

        • AaronW
          New Member
          • Nov 2007
          • 4

          #5
          I would prefer to change the background color if possible.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Can't you set that on DataGridViewRow object?

            Comment

            • AaronW
              New Member
              • Nov 2007
              • 4

              #7
              Got it with:

              private void AddCellFormatti ngColumnStyles( DataGrid grid, FormatCellEvent Handler handler)
              {
              DataGridTableSt yle ts = new DataGridTableSt yle();
              DataTable dt = (DataTable) grid.DataSource ;
              ts.MappingName = dt.TableName;

              for(int j = 0; j < dt.Columns.Coun t; ++j)
              {
              DataGridFormatt ableTextBoxColu mn cs = new DataGridFormatt ableTextBoxColu mn(j);
              cs.MappingName = dt.Columns[j].ColumnName;
              cs.HeaderText = dt.Columns[j].ColumnName;
              cs.SetCellForma t += handler;
              ts.GridColumnSt yles.Add(cs);
              }

              //grid.TableStyle s.Clear();
              grid.TableStyle s.Add(ts);

              }

              private void FormatGridCells (object sender, DataGridFormatC ellEventArgs e)
              {
              CurrencyManager cm = (CurrencyManage r)this.BindingC ontext[this.dgMain.Dat aSource];

              object cellValue = this.dgMain[e.Row, 4];
              if (cellValue.ToSt ring() == "ACTION REQUIRED")
              {
              e.BackBrush = Brushes.Red;
              }
              }

              Comment

              Working...