Setting Foreground Color Property at Row Level in Datagrid

F

fripper

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.
 
C

Chris

fripper said:
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
 
F

fripper

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.
 
C

Chris

fripper said:
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.

In 2005 you can use the DataGridView which makes everything much much
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top