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. DataBinding Row DataGrid

DataBinding Row DataGrid

Scheduled Pinned Locked Moved C#
question
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.
  • B Offline
    B Offline
    Braulio Dez
    wrote on last edited by
    #1

    Hi, I have a datagrid with several columns, one is "Finish Date", I would like that the rows that has a finish date greater than the current data have a different background color that the other one. I have thought about create a calculated, hidden column, that will hold the column of the row, it's possible then bind that calculated column, to the background color of the row ?, Is there another easier way to do it ? thanks in advance, greetings Braulio

    H 1 Reply Last reply
    0
    • B Braulio Dez

      Hi, I have a datagrid with several columns, one is "Finish Date", I would like that the rows that has a finish date greater than the current data have a different background color that the other one. I have thought about create a calculated, hidden column, that will hold the column of the row, it's possible then bind that calculated column, to the background color of the row ?, Is there another easier way to do it ? thanks in advance, greetings Braulio

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      The unfortunate truth is that in ASP.NET, doing this with a DataGrid (System.Web.UI.WebControls.DataGrid, that is) is incredibly easier. You handle the ItemDataBound event, which gives you the item (the object being bound) to which you can set properties based on the data being bound, such as different font styles, colors, etc. Easy, huh? I'm guessing you're not doing this in ASP.NET, though. Now it gets harder. The best way is to derive your own class from DataGridColumnStyle (or perhaps even form DataGridTextBoxColumn which you're most likely already using for the column style). If you choose the latter, it already exposes the TextBox that is hosted when you edit the cell. Note, though, that it is not enough to set the BackColor of the TextBox because it's only visible when the cell is being edited. So, after deciding which you want to do (I recommend the latter), override the Paint method and - based on the value bound to the cell - paint the appropriate background. The only thing left to do is modify the DataGrid initialization code. If you're not already using a DataGridTableStyle, you should because it gives you a high degree of control over the display and is still versitile enough to handle different data sources. See the DataGrid.TableStyles property for more info. To this DataGridTableStyle, you add your various DataGridColumnStyle derivatives, such as the DataGridTextBoxColumn, the DataGridCheckBoxColumn, and your new-fangled derivative you created from above. If you already have one, just modified the source for that column to use your object instead of the DataGridTextBoxColumn instance. It may sound like quite a bit of work, but this is an extremely versitile solution. Instead of handling special cases that require code every time you run into this problem, you make a single class that you can easily reuse - which is what component development is all about! If you create your own little library of handy classes like this, you won't even have to copy and paste source files from project to project - just reference the library assembly and specify the type. In the end, it definitely pays off and is "in the spirit" of the .NET base class library, i.e. extending it rather than working around it.

      B 1 Reply Last reply
      0
      • H Heath Stewart

        The unfortunate truth is that in ASP.NET, doing this with a DataGrid (System.Web.UI.WebControls.DataGrid, that is) is incredibly easier. You handle the ItemDataBound event, which gives you the item (the object being bound) to which you can set properties based on the data being bound, such as different font styles, colors, etc. Easy, huh? I'm guessing you're not doing this in ASP.NET, though. Now it gets harder. The best way is to derive your own class from DataGridColumnStyle (or perhaps even form DataGridTextBoxColumn which you're most likely already using for the column style). If you choose the latter, it already exposes the TextBox that is hosted when you edit the cell. Note, though, that it is not enough to set the BackColor of the TextBox because it's only visible when the cell is being edited. So, after deciding which you want to do (I recommend the latter), override the Paint method and - based on the value bound to the cell - paint the appropriate background. The only thing left to do is modify the DataGrid initialization code. If you're not already using a DataGridTableStyle, you should because it gives you a high degree of control over the display and is still versitile enough to handle different data sources. See the DataGrid.TableStyles property for more info. To this DataGridTableStyle, you add your various DataGridColumnStyle derivatives, such as the DataGridTextBoxColumn, the DataGridCheckBoxColumn, and your new-fangled derivative you created from above. If you already have one, just modified the source for that column to use your object instead of the DataGridTextBoxColumn instance. It may sound like quite a bit of work, but this is an extremely versitile solution. Instead of handling special cases that require code every time you run into this problem, you make a single class that you can easily reuse - which is what component development is all about! If you create your own little library of handy classes like this, you won't even have to copy and paste source files from project to project - just reference the library assembly and specify the type. In the end, it definitely pays off and is "in the spirit" of the .NET base class library, i.e. extending it rather than working around it.

        B Offline
        B Offline
        Braulio Dez
        wrote on last edited by
        #3

        Thanks for your answer. I'm using windows forms, well a bit of work to make that, it's a pity that the VBA get effects like this so fast, and we have to spend some time into it ( how can you explain to that guy that the first time you need an hour to make a thing that he makes in five minutes :-( ). Another problem that I found on the Windows Form Datagrid, is that there not an easy way to fake the load of a result ( I mean, load only the first 1000 entries, then by demand load the others or all, something like that). Greetings Braulio

        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