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