In Form1.vb Code
Imports System.Data Imports System.Data.SqlClient Public Class Form1 Inherits System.Windows.Forms.Form Dim ds As New DataSet
Form1_Load Event u can write this code: This event can call the myDataSet and AddCellFormattingColumnStyles procedures. myDataSet() AddCellFormattingColumnStyles(Me.DataGrid1, New FormatCellEventHandler(AddressOf FormatGridCells))
to Creata Sub Procedure name as Protected Sub myDataSet() to establish a database connection. and to bind the data to DataGrid Control Dim con As SqlConnection Dim da As SqlDataAdapter Try con = New SqlConnection("Server=Server;database=pubs;user id=sa;pwd=sa") con.Open() da = New SqlDataAdapter("select * from persons", con) da.Fill(ds, "persons") DataGrid1.DataSource = ds.Tables(0).DefaultView Catch ex As Exception MsgBox(ex.Message) Finally con.Close() con.Dispose() End Try
Private Sub FormatGridCells(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs) This is for for row, here we can assing the Row No. to Color. If u want u can implement ur logics here...
If (e.Row = 1) Then e.BackBrush = Brushes.Cyan End If
End Sub
Private Sub AddCellFormattingColumnStyles(ByVal grid As DataGrid, ByVal handler As FormatCellEventHandler)
Dim ts As DataGridTableStyle ts = New DataGridTableStyle Dim dt As DataTable dt = CType(ds.Tables(0), DataTable) ts.MappingName = dt.TableName Dim j As Integer j = 0
Do While (j < dt.Columns.Count) Dim cs As DataGridFormattableTextBoxColumn cs = New DataGridFormattableTextBoxColumn(j) cs.MappingName = dt.Columns(j).ColumnName cs.HeaderText = dt.Columns(j).ColumnName AddHandler cs.SetCellFormat, handler ts.GridColumnStyles.Add(cs) j = (j + 1)
Loop grid.TableStyles.Clear() grid.TableStyles.Add(ts)
End Sub
Add One New Module and give name as DataGridFormatCellEventArgs.vb and write the some code In This module we can implement the properties to set the value and get the values .
Option Strict Off Option Explicit On Imports Microsoft.VisualBasic Imports System Imports System.Drawing
Public Class DataGridFormatCellEventArgs Inherits EventArgs Private column As Integer Private row As Integer Private font As Font Private backBrush As Brush Private foreBrush As Brush Private useBaseClassDrawing As Boolean
Public Sub New(ByVal row As Integer, ByVal col As Integer, ByVal font1 As Font, ByVal backBrush As Brush, ByVal foreBrush As Brush) MyBase.New() row = row column = col font = font1 backBrush = backBrush foreBrush = foreBrush useBaseClassDrawing = False
End Sub Public Property Column() As Integer Get Return column End Get Set(ByVal Value As Integer) column = Value End Set End Property
Public Property Row() As Integer Get Return row End Get Set(ByVal Value As Integer) row = Value End Set End Property
Public Property TextFont() As Font Get Return font End Get Set(ByVal Value As Font) font = Value End Set End Property Public Property BackBrush() As Brush Get Return backBrush End Get Set(ByVal Value As Brush) backBrush = Value End Set End Property
Public Property ForeBrush() As Brush Get Return foreBrush End Get Set(ByVal Value As Brush) foreBrush = Value End Set End Property
Public Property UseBaseClassDrawing() As Boolean Get Return useBaseClassDrawing End Get Set(ByVal Value As Boolean) useBaseClassDrawing = Value End Set End Property End Class
Add One New Item Comoponet File give the name DataGridFormattableTextBoxColumn and write the below code: In this Overriding the Paint Method.
Option Strict Off Option Explicit On Imports Microsoft.VisualBasic Imports System Imports System.Drawing Imports System.Windows.Forms
Public Class DataGridFormattableTextBoxColumn Inherits DataGridTextBoxColumn Private col As Integer
Public Sub New(ByVal col As Integer) MyBase.New() col = col
End Sub
Public Event SetCellFormat As FormatCellEventHandler
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)
Dim e As DataGridFormatCellEventArgs e = New DataGridFormatCellEventArgs(rowNum, Me.col, Me.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush) RaiseEvent SetCellFormat(Me, e) If e.UseBaseClassDrawing Then MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) Else g.FillRectangle(e.BackBrush, bounds) g.DrawString(Me.GetColumnValueAtRow(source, rowNum).ToString, e.TextFont, e.ForeBrush, bounds.X, bounds.Y) End If If (e.TextFont Is Me.DataGridTableStyle.DataGrid.Font) = False Then e.TextFont.Dispose() End If
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [ReadOnly], instantText, cellIsVisible)
End Sub End Class
Add One new Item module give the name as FormatCellEventHandler and write below code. In this declare a Delegate
Option Strict Off Option Explicit On
Imports Microsoft.VisualBasic Imports System
Public Delegate Sub FormatCellEventHandler(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|