Datagrid, DataSet
-
In the present code, we use a datagrid as a container to print data from a SQL database. The following gets data (via ODBC) in the SQL database and save it in a DATASET. Public Function GetDatasetGiveSql(ByVal sqlCmd As String) As DataSet Dim dataset As New DataSet odbcCmd.Connection.Close() odbcCmd = New Odbc.OdbcCommand(sqlCmd, odbcConnection) odbcAdapt = New Odbc.OdbcDataAdapter(odbcCmd) odbcCmd.Connection.Open() odbcAdapt.Fill(dataset) Return dataset End Function Elsewhere in the software, the software builds a query and sends it to the database like this: dsCommand = sql.GetDatasetGiveSql(cmd) DataGridCommand.SetDataBinding(dsCommand, "Table") As you can suspect, dsCommand is a DataSet and DataGridCommand is the DataGrid to update. Easy to date… OK. Question: --------- I want to have control on the background and foreground color of a cell based on some information like any people in this forum. I found many EXAMPLES with DataGridColumnStyle where the idea is to override the Paint method (aka: make a custom datagrid control). Yes, Perfect. I understand it BUT in this case… I DON’T CONTROL ANY COLUMN in the datagrid. It seems that the SetDataBinding() does everything by itself, it is magic. I did not find HOW to OVERRIDE the Paint() method of this datagrid. Maybe I don’t understand something ! :^) Danny Gilbert, enginneer Montréal, Canada Danny Gilbert, enginneer Montréal, Canada
-
In the present code, we use a datagrid as a container to print data from a SQL database. The following gets data (via ODBC) in the SQL database and save it in a DATASET. Public Function GetDatasetGiveSql(ByVal sqlCmd As String) As DataSet Dim dataset As New DataSet odbcCmd.Connection.Close() odbcCmd = New Odbc.OdbcCommand(sqlCmd, odbcConnection) odbcAdapt = New Odbc.OdbcDataAdapter(odbcCmd) odbcCmd.Connection.Open() odbcAdapt.Fill(dataset) Return dataset End Function Elsewhere in the software, the software builds a query and sends it to the database like this: dsCommand = sql.GetDatasetGiveSql(cmd) DataGridCommand.SetDataBinding(dsCommand, "Table") As you can suspect, dsCommand is a DataSet and DataGridCommand is the DataGrid to update. Easy to date… OK. Question: --------- I want to have control on the background and foreground color of a cell based on some information like any people in this forum. I found many EXAMPLES with DataGridColumnStyle where the idea is to override the Paint method (aka: make a custom datagrid control). Yes, Perfect. I understand it BUT in this case… I DON’T CONTROL ANY COLUMN in the datagrid. It seems that the SetDataBinding() does everything by itself, it is magic. I did not find HOW to OVERRIDE the Paint() method of this datagrid. Maybe I don’t understand something ! :^) Danny Gilbert, enginneer Montréal, Canada Danny Gilbert, enginneer Montréal, Canada
Hi Danny I found a class on the net that changes the cell color according to what information is placed in the cell, is this what your after? Below is how I used it. Instead of a basic color I actually used a linear graded color in the cell, looks great in the grid. Imports Microsoft.VisualBasic Imports System Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Public Class DataGridColoredTextBoxColumn Inherits DataGridTextBoxColumn Public Sub New() 'Warning: Implementation not found End Sub Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean) ' the idea is to conditionally set the foreBrush and/or backbrush ' depending upon some crireria on the cell value ' Here,we color anything that matches the select case Try Dim o As Object o = Me.GetColumnValueAtRow(source, rowNum) If (Not (o) Is Nothing) Then Dim c As Char Dim cellStr As String c = CType(o, String).Substring(0, 1) cellStr = CType(o, String) Select Case cellStr Case "**CRITICAL**" backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 50, 25), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal) foreBrush = New SolidBrush(Color.White) Case "- URGENT -" backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 255, 255, 0), Color.FromArgb(200, 10, 0), LinearGradientMode.BackwardDiagonal) foreBrush = New SolidBrush(Color.White) Case "current" backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 10, 255, 0), Color.FromArgb(0, 50, 0), LinearGradientMode.BackwardDiagonal) foreBrush = New SolidBrush(Color.White) End Select End If Catch ex As Exception ' empty catch Finally ' make sure the base class gets called to do the drawing with ' the possibly changed brushes MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) End Try End Sub End Class It gets called
-
Hi Danny I found a class on the net that changes the cell color according to what information is placed in the cell, is this what your after? Below is how I used it. Instead of a basic color I actually used a linear graded color in the cell, looks great in the grid. Imports Microsoft.VisualBasic Imports System Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Public Class DataGridColoredTextBoxColumn Inherits DataGridTextBoxColumn Public Sub New() 'Warning: Implementation not found End Sub Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean) ' the idea is to conditionally set the foreBrush and/or backbrush ' depending upon some crireria on the cell value ' Here,we color anything that matches the select case Try Dim o As Object o = Me.GetColumnValueAtRow(source, rowNum) If (Not (o) Is Nothing) Then Dim c As Char Dim cellStr As String c = CType(o, String).Substring(0, 1) cellStr = CType(o, String) Select Case cellStr Case "**CRITICAL**" backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 50, 25), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal) foreBrush = New SolidBrush(Color.White) Case "- URGENT -" backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 255, 255, 0), Color.FromArgb(200, 10, 0), LinearGradientMode.BackwardDiagonal) foreBrush = New SolidBrush(Color.White) Case "current" backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 10, 255, 0), Color.FromArgb(0, 50, 0), LinearGradientMode.BackwardDiagonal) foreBrush = New SolidBrush(Color.White) End Select End If Catch ex As Exception ' empty catch Finally ' make sure the base class gets called to do the drawing with ' the possibly changed brushes MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) End Try End Sub End Class It gets called
Thank you very much, I will try it. :) Danny Gilbert, enginneer Montréal, Canada
-
Thank you very much, I will try it. :) Danny Gilbert, enginneer Montréal, Canada