|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Setting Foreground Color Property at Row Level in DatagridI think I am using the proper terminology here ... I am using VB 2003 and
have a DataGrid to which I then attach a TableStyle ... then I set the properties of the TableStyle (BackColor, HeaderBackColor, etc.) ... then I create several DataGridTextBoxColumns and set their properties (MappingName, Width, Alignment, etc.) ... then I add these columns to the DataGridStyle. Next, using the MappingNames I add the columns to a DataTable ... then I add the DataTable to a DataSet ... and finally bind the DataSet to the DataGrid so the user can see it (whew!). All of this seems incredibly complicated when all I want to do is display some stuff in tabular form with some column headers. In any event, I would like to set the foreground color of a displayed row based on the value of a particular item in the row ... yet with all these constructs (table styles, textbox columns, data tables, etc.) I cannot figure out how to do it. Can someone help me understand how to do this ... or point me to an article that will help? Thanks very much. fripper wrote:
Show quoteHide quote > I think I am using the proper terminology here ... I am using VB 2003 and Yes, your terminology is correct. I will say you could simplify it by > have a DataGrid to which I then attach a TableStyle ... then I set the > properties of the TableStyle (BackColor, HeaderBackColor, etc.) ... then I > create several DataGridTextBoxColumns and set their properties (MappingName, > Width, Alignment, etc.) ... then I add these columns to the DataGridStyle. > Next, using the MappingNames I add the columns to a DataTable ... then I add > the DataTable to a DataSet ... and finally bind the DataSet to the DataGrid > so the user can see it (whew!). All of this seems incredibly complicated > when all I want to do is display some stuff in tabular form with some column > headers. > > In any event, I would like to set the foreground color of a displayed row > based on the value of a particular item in the row ... yet with all these > constructs (table styles, textbox columns, data tables, etc.) I cannot > figure out how to do it. Can someone help me understand how to do this ... > or point me to an article that will help? > > Thanks very much. > > binding the datatable directly to the datagrid (make sure the tablename equals the tablestyle.mappingname.) But now to your next question. Unfortunately to do this you have to create your own DataGridTextBoxColumns and override the onpaint method. It's not as hard as you might think if you've never done it before. This article walks you through it (it's in C# but the function calls are all there) http://www.codeproject.com/csharp/Custom_DataGridColumnStyl.asp The important function is: protected override sub Paint(....) public sub dim bdel as boolean= ctype(GetColumnValueAtRow(Source, RowNum), bool) if bdel then BackBrush = Brushes.Coral else BackBrush = Brushes.White end if g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height) System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.25 ) g.DrawString( bdel.ToString(), font, Brushes.Black, Bounds.X, Bounds.Y) end sub Thanks Chris ... that is very helpful. I have modified the program to bind
the datatable directly to the datagrid ... at least that eliminates one unnecessary construct in the process. Now I will see about adding my own onPaint method. It's a slow process but I am gradually becoming more familiar with VB .Net. I have just installed VB 2005 but have not yet ported this program over to it. Thanks again. Show quoteHide quote "Chris" <no@spam.com> wrote in message news:ukmyEVHSGHA.5900@tk2msftngp13.phx.gbl... > fripper wrote: >> I think I am using the proper terminology here ... I am using VB 2003 and >> have a DataGrid to which I then attach a TableStyle ... then I set the >> properties of the TableStyle (BackColor, HeaderBackColor, etc.) ... then >> I create several DataGridTextBoxColumns and set their properties >> (MappingName, Width, Alignment, etc.) ... then I add these columns to the >> DataGridStyle. Next, using the MappingNames I add the columns to a >> DataTable ... then I add the DataTable to a DataSet ... and finally bind >> the DataSet to the DataGrid so the user can see it (whew!). All of this >> seems incredibly complicated when all I want to do is display some stuff >> in tabular form with some column headers. >> >> In any event, I would like to set the foreground color of a displayed row >> based on the value of a particular item in the row ... yet with all these >> constructs (table styles, textbox columns, data tables, etc.) I cannot >> figure out how to do it. Can someone help me understand how to do this >> ... or point me to an article that will help? >> >> Thanks very much. > > Yes, your terminology is correct. I will say you could simplify it by > binding the datatable directly to the datagrid (make sure the tablename > equals the tablestyle.mappingname.) But now to your next question. > > Unfortunately to do this you have to create your own > DataGridTextBoxColumns and override the onpaint method. It's not as hard > as you might think if you've never done it before. This article walks you > through it (it's in C# but the function calls are all there) > > http://www.codeproject.com/csharp/Custom_DataGridColumnStyl.asp > > The important function is: > > protected override sub Paint(....) > > public sub > dim bdel as boolean= ctype(GetColumnValueAtRow(Source, RowNum), bool) > > if bdel then > BackBrush = Brushes.Coral > else > BackBrush = Brushes.White > end if > > g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, > Bounds.Height) > > System.Drawing.Font font = new > Font(System.Drawing.FontFamily.GenericSansSerif, > (float)8.25 ) > g.DrawString( bdel.ToString(), font, Brushes.Black, Bounds.X, > Bounds.Y) > > end sub fripper wrote:
Show quoteHide quote > Thanks Chris ... that is very helpful. I have modified the program to bind In 2005 you can use the DataGridView which makes everything much much > the datatable directly to the datagrid ... at least that eliminates one > unnecessary construct in the process. > > Now I will see about adding my own onPaint method. It's a slow process but > I am gradually becoming more familiar with VB .Net. I have just installed > VB 2005 but have not yet ported this program over to it. > > Thanks again. > > > "Chris" <no@spam.com> wrote in message > news:ukmyEVHSGHA.5900@tk2msftngp13.phx.gbl... > >>fripper wrote: >> >>>I think I am using the proper terminology here ... I am using VB 2003 and >>>have a DataGrid to which I then attach a TableStyle ... then I set the >>>properties of the TableStyle (BackColor, HeaderBackColor, etc.) ... then >>>I create several DataGridTextBoxColumns and set their properties >>>(MappingName, Width, Alignment, etc.) ... then I add these columns to the >>>DataGridStyle. Next, using the MappingNames I add the columns to a >>>DataTable ... then I add the DataTable to a DataSet ... and finally bind >>>the DataSet to the DataGrid so the user can see it (whew!). All of this >>>seems incredibly complicated when all I want to do is display some stuff >>>in tabular form with some column headers. >>> >>>In any event, I would like to set the foreground color of a displayed row >>>based on the value of a particular item in the row ... yet with all these >>>constructs (table styles, textbox columns, data tables, etc.) I cannot >>>figure out how to do it. Can someone help me understand how to do this >>>... or point me to an article that will help? >>> >>>Thanks very much. >> >>Yes, your terminology is correct. I will say you could simplify it by >>binding the datatable directly to the datagrid (make sure the tablename >>equals the tablestyle.mappingname.) But now to your next question. >> >>Unfortunately to do this you have to create your own >>DataGridTextBoxColumns and override the onpaint method. It's not as hard >>as you might think if you've never done it before. This article walks you >>through it (it's in C# but the function calls are all there) >> >>http://www.codeproject.com/csharp/Custom_DataGridColumnStyl.asp >> >>The important function is: >> >>protected override sub Paint(....) >> >>public sub >> dim bdel as boolean= ctype(GetColumnValueAtRow(Source, RowNum), bool) >> >> if bdel then >> BackBrush = Brushes.Coral >> else >> BackBrush = Brushes.White >> end if >> >> g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, >>Bounds.Height) >> >> System.Drawing.Font font = new >>Font(System.Drawing.FontFamily.GenericSansSerif, >> (float)8.25 ) >> g.DrawString( bdel.ToString(), font, Brushes.Black, Bounds.X, >>Bounds.Y) >> >>end sub > > > easier. If you're considering the upgrade you may want to do it sooner and switch over the control. Note: The DataGridView is for viewing one table only, unlike the DataGrid which can show off relationships. Chris |
|||||||||||||||||||||||