Click here to Skip to main content
15,891,375 members
Home / Discussions / C#
   

C#

 
GeneralRe: RichTextBox contents in a DB Pin
Joel Lucsy3-Dec-03 8:43
Joel Lucsy3-Dec-03 8:43 
GeneralRe: Creating a new database... Pin
Heath Stewart3-Dec-03 5:15
protectorHeath Stewart3-Dec-03 5:15 
GeneralCreating a new database... Pin
Amirjalaly3-Dec-03 5:09
Amirjalaly3-Dec-03 5:09 
QuestionHow to display text the same way as printed? Pin
Loke13-Dec-03 3:59
Loke13-Dec-03 3:59 
GeneralCultureInfo Pin
Member 7439443-Dec-03 2:43
Member 7439443-Dec-03 2:43 
GeneralRe: CultureInfo Pin
Heath Stewart3-Dec-03 4:30
protectorHeath Stewart3-Dec-03 4:30 
GeneralDataBinding Row DataGrid Pin
Braulio Dez3-Dec-03 2:32
Braulio Dez3-Dec-03 2:32 
GeneralRe: DataBinding Row DataGrid Pin
Heath Stewart3-Dec-03 4:15
protectorHeath Stewart3-Dec-03 4:15 
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.

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: DataBinding Row DataGrid Pin
Braulio Dez3-Dec-03 21:06
Braulio Dez3-Dec-03 21:06 
Generalchat server Pin
Mahmoud Manasrah3-Dec-03 1:20
sussMahmoud Manasrah3-Dec-03 1:20 
Generalchat server Pin
Mahmoud Manasrah3-Dec-03 1:19
sussMahmoud Manasrah3-Dec-03 1:19 
GeneralRe: chat server Pin
Heath Stewart3-Dec-03 3:57
protectorHeath Stewart3-Dec-03 3:57 
GeneralRe: chat server Pin
Mahmoud Manasrah3-Dec-03 20:50
sussMahmoud Manasrah3-Dec-03 20:50 
GeneralCan't paste a image to PicBox Pin
trythat3-Dec-03 1:05
trythat3-Dec-03 1:05 
GeneralRe: Can't paste a image to PicBox Pin
Heath Stewart3-Dec-03 3:51
protectorHeath Stewart3-Dec-03 3:51 
GeneralRe: Can't paste a image to PicBox Pin
trythat3-Dec-03 6:55
trythat3-Dec-03 6:55 
GeneralRe: Can't paste a image to PicBox Pin
Heath Stewart3-Dec-03 8:16
protectorHeath Stewart3-Dec-03 8:16 
GeneralRe: Can't paste a image to PicBox Pin
trythat4-Dec-03 0:51
trythat4-Dec-03 0:51 
GeneralRe: Can't paste a image to PicBox Pin
Heath Stewart4-Dec-03 5:07
protectorHeath Stewart4-Dec-03 5:07 
GeneralRe: Can't paste a image to PicBox Pin
trythat4-Dec-03 5:16
trythat4-Dec-03 5:16 
GeneralC# MIME parser Pin
pahlsson3-Dec-03 0:53
pahlsson3-Dec-03 0:53 
GeneralRe: C# MIME parser Pin
Heath Stewart3-Dec-03 3:46
protectorHeath Stewart3-Dec-03 3:46 
GeneralRe: C# MIME parser Pin
Ivar Lumi3-Dec-03 4:12
Ivar Lumi3-Dec-03 4:12 
GeneralPerformance Java vs. C# Pin
MeisterBiber3-Dec-03 0:36
MeisterBiber3-Dec-03 0:36 
GeneralRe: Performance Java vs. C# Pin
Heath Stewart3-Dec-03 3:41
protectorHeath Stewart3-Dec-03 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.