Dynamic datagrid

T

tg

Visual Studio 2003

My problem is that I am dynamically creating a datagrid on pageload.
The datagrid is completely dynamic as it is based on the number of columns
returned from a recordset. The number of columns could change very often.
Within this datagrid that is created, each cell needs to be colored
different colors based on a the value within the cell. For example, if the
number is less than 90, color the cell red, if it is greater than 90, color
is yellow, etc.

The datagrid object is not created cell by cell, but by columns, and we do
not need the entire column colored red, green, yellow, etc. So, we need to
somehow override the render function, but we have absolutely no idea how to
do this.

I am new at this, and I am trying, but I need some help.

My code is:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strSql As String = "exec dbo.uspSummaryMatrix"
Dim strCnn As String = "data source=PEREZ-TOMMY-G;integrated
security=false;initial catalog=COPS;"
objCnn = New SqlConnection(strCnn)
objCmd = New SqlCommand
objCmd.CommandText = "dbo.uspSummaryMatrix"
objCmd.CommandType = CommandType.StoredProcedure
objCmd.Connection = objCnn
objCmd.Parameters.Add(New SqlParameter("@ItemID",
System.Data.SqlDbType.VarChar, 255))

objCmd.Parameters("@ItemID").Value = Request.QueryString("ID")

objCnn.Open()

Dim dReader As SqlDataReader
dReader = Nothing
dReader = objCmd.ExecuteReader()

Dim i As Integer
Dim count As Integer
count = 0
Dim DataGrid1 As New DataGrid
DataGrid1.GridLines = GridLines.Both
DataGrid1.AllowSorting = True
DataGrid1.BorderColor = Color.SlateGray
DataGrid1.BorderStyle = BorderStyle.Solid
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.BackColor = Color.White
DataGrid1.AlternatingItemStyle.BackColor = Color.LightGray
DataGrid1.ItemStyle.ForeColor = Color.DarkSlateBlue
DataGrid1.ItemStyle.BackColor = Color.White
DataGrid1.HeaderStyle.Font.Bold = True
DataGrid1.HeaderStyle.ForeColor = Color.White
DataGrid1.HeaderStyle.BackColor = Color.DarkSlateBlue
DataGrid1.Width = Unit.Percentage(100)
DataGrid1.CellPadding = 2
Dim datagridcol
Dim strcolor As String

Do While dReader.Read
If count = 0 Then
For i = 0 To dReader.FieldCount - 1
If InStr(dReader.GetName(i), "container") Then
datagridcol = New BoundColumn
datagridcol.headertext = Replace(dReader.GetName(i),
"c", "C")
datagridcol.datafield = dReader.GetName(i)
DataGrid1.Columns.Add(datagridcol)
End If
If InStr(dReader.GetName(i), "Word") Then
datagridcol = New HyperLinkColumn
datagridcol.HeaderText = Replace(dReader.GetName(i),
"Word", "")
datagridcol.DatatextField = dReader.GetName(i)
End If
If InStr(dReader.GetName(i), "Color") Then
'set color of CELL here
strcolor = dReader.GetString(i)
End If
If InStr(dReader.GetName(i), "Link") Then
datagridcol.datanavigateurlfield =
dReader.GetName(i)
datagridcol.datanavigateurlformatstring =
"UsageDocuments2.aspx?ID={0}"
datagridcol.target = "iframedocument"
DataGrid1.Columns.Add(datagridcol)
End If
Next
End If
count = count + 1
Loop

dReader.Close()

objDataAdapter = New SqlDataAdapter(objCmd)
objDataSet = New DataSet
objDataView = New DataView
objDataAdapter.Fill(objDataSet)
objDataView.Table = objDataSet.Tables(0)

DataGrid1.DataSource = objDataView
DataGrid1.DataBind()

'add datagrid to the page
Me.Controls.Add(DataGrid1)

objCnn.Close()

End Sub
 
G

Guest

You would probably traverse the grid with a nested loop first and set your
colors for each cell as you tested each cell's value using the DataGrid.item
property to get the value of the cell. Then you would hold the cell's value
as a string, and redraw the cell using a graphics object, first filling the
color, then re-setting the value with drawstring.

Dim s As String = Me.DataGrid1.Item(1, 2)
Dim r As Rectangle = Me.DataGrid1.GetCellBounds(1, 2)
Dim g As Graphics = Me.DataGrid1.CreateGraphics
Dim sb As New SolidBrush(Color.Yellow)
g.FillRectangle(sb, r)
sb.Color = Color.Black
g.DrawString(s, Me.DataGrid1.Font, sb, r.X, r.Y)

Note that in DrawString, the Y coordinate may displace the string slightly.
I found I had to add 2 to the Y value to keep the cell's value in the
original position.

www.charlesfarriersoftware.com
 
G

Guest

Also, you might want to use a With statement when you have an object repeated
for several lines, in this case DataGrid1. Your code will process more
efficiently and look better.
 
C

Cor Ligthert

Charlie,
Also, you might want to use a With statement when you have an object
repeated
for several lines, in this case DataGrid1. Your code will process more
efficiently and look better.
You did probably not follow the big discussion about that last week, where
the outcome of your first statement (tested by Jay) is that this is not
true, there is no measurable difference when it is used as exe/dll/. When it
is started in the IDE there can be a slight difference mostly better
sometimes slower.

(The last part of your sentence is a mater of preference and by instance I
find it awfull when I a "with" statement is used, when it is done in other
cases as by instance with Office Interop, where the object path is very
long)

Cor
 

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