Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Color a row in a Datagrid, is there a solution that works with DataView?

Color a row in a Datagrid, is there a solution that works with DataView?

Scheduled Pinned Locked Moved C#
questionhelptutorialcsharp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cesa37
    wrote on last edited by
    #1

    I have found one solution for how to color a row in a DataGrid based on the data in the row. Here's one example: guffy.net[^]. The problem is that it doesn't work when the DataGrid is sorted, or when a RowFilter is applied. Example: I have a simple table with two columns, one with integer values and one with strings. 15 Some text -14 Different text 5 Another string 6 Yet another string Say I want all rows where the integer is negative to have a blue background. This is easily done using the example above. In this example the second row will be colored blue. But say I don't want to show any rows with an integer >= 10. When I create the DataGrid I use the code DataView mainDataView = ((DataTable)this.mainDataGrid.DataSource).DefaultView; mainDataView.RowFilter = "[Value] < 10"; Now the DataGrid will look like: -14 Different text 5 Another string 6 Yet another string But now the second row will have a blue background, instead of the first. How can I solve this problem? Any help is appriciated! (btw, I already asked this question in an old thread, but I'm new here and thought a thread would get "bumped" when it got a new message, turns out that the message bord is sorted by the first message in a thread, so I guess noone noticed that message...) /Cesa

    R 1 Reply Last reply
    0
    • C Cesa37

      I have found one solution for how to color a row in a DataGrid based on the data in the row. Here's one example: guffy.net[^]. The problem is that it doesn't work when the DataGrid is sorted, or when a RowFilter is applied. Example: I have a simple table with two columns, one with integer values and one with strings. 15 Some text -14 Different text 5 Another string 6 Yet another string Say I want all rows where the integer is negative to have a blue background. This is easily done using the example above. In this example the second row will be colored blue. But say I don't want to show any rows with an integer >= 10. When I create the DataGrid I use the code DataView mainDataView = ((DataTable)this.mainDataGrid.DataSource).DefaultView; mainDataView.RowFilter = "[Value] < 10"; Now the DataGrid will look like: -14 Different text 5 Another string 6 Yet another string But now the second row will have a blue background, instead of the first. How can I solve this problem? Any help is appriciated! (btw, I already asked this question in an old thread, but I'm new here and thought a thread would get "bumped" when it got a new message, turns out that the message bord is sorted by the first message in a thread, so I guess noone noticed that message...) /Cesa

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      You should change these two lines

      DataRowView v = (DataRowView)source.Current;
      string curColumnValue = Convert.ToString(v.DataView.Table.Rows[rowNum][columnName]);

      to

      DataRowView v = (DataRowView)source.Current;
      string curColumnValue = Convert.ToString(v.Row[columnName]);

      A DataRowView knows its associated DataRow. Thus you don't have to look into the DataTable.

      C 1 Reply Last reply
      0
      • R Robert Rohde

        You should change these two lines

        DataRowView v = (DataRowView)source.Current;
        string curColumnValue = Convert.ToString(v.DataView.Table.Rows[rowNum][columnName]);

        to

        DataRowView v = (DataRowView)source.Current;
        string curColumnValue = Convert.ToString(v.Row[columnName]);

        A DataRowView knows its associated DataRow. Thus you don't have to look into the DataTable.

        C Offline
        C Offline
        Cesa37
        wrote on last edited by
        #3

        Thanks, but for some reason I can't get it to work. To be honest I didn't look into it that much since I managed to find another solution earlier. I modified your example to my actual project: DataRowView v = (DataRowView)source.Current; DateTime date = (DateTime)v.Row["Date"]; ...but it doesn't work. This works fine though: In class: //public version of GetColumnValueAtRow public object PublicGetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, System.Int32 rowNum) { return this.GetColumnValueAtRow(source, rowNum); } In Paint(...): DateTime date = (DateTime)((MyDataGridTextBoxColumn)this.DataGridTableStyle.GridColumnStyles["Date"]).PublicGetColumnValueAtRow(source, rowNum); -- modified at 12:46 Tuesday 17th January, 2006

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups