Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Datagrid, DataSet

Datagrid, DataSet

Scheduled Pinned Locked Moved Visual Basic
databasedockertutorialquestionannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dannygilbert3
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • D dannygilbert3

      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

      C Offline
      C Offline
      cowtech
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • C cowtech

        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

        D Offline
        D Offline
        dannygilbert3
        wrote on last edited by
        #3

        Thank you very much, I will try it. :) Danny Gilbert, enginneer Montréal, Canada

        C 1 Reply Last reply
        0
        • D dannygilbert3

          Thank you very much, I will try it. :) Danny Gilbert, enginneer Montréal, Canada

          C Offline
          C Offline
          cowtech
          wrote on last edited by
          #4

          Hi Danny Just wondering how you went with this. cowtech Sydney Australia

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups