AboutSyed Rizwan Muhammad Rizvi Expertise I can answers questions regarding web based and desktop based programming in VB.Net. Which can include SOAP, XML, Custom Controls, COM Interoperability etc.
Experience Have been working in this specific area for last 2 years previously I was a VB 6 Developer with experties in other languages as well. Total 10 years of programming experience.
Question QUESTION:
Hi
I want to retrieve images from Access Database and display it in Datagrid.
Columns:
========
ID
Name
Photo
I have used Datagrid columnstyle for this.Everything is displaying well but image is Overlapping on the text column.
Could u pls help me out it is very very urgent.............
ANSWER: please paste the grid code here.
---------- FOLLOW-UP ----------
QUESTION: Thanks I have cleared it.If I use Bitmap Iam bale to view clearly.If it is JPEG I get that problem.Could you help me out how to add a button colum in the data grid.
Public Class Form4
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents DataSet11 As App1.DataSet1
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter
Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand
Me.OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand
Me.OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand
Me.DataSet11 = New App1.DataSet1
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(24, 32)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(648, 424)
Me.DataGrid1.TabIndex = 0
'
'OleDbConnection1
'
Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Data Source=""C:\Documents and Settings\Administrator\My Documents\" & _
"MMT\Vote.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.J" & _
"et.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security in" & _
"fo=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet" & _
" OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:D" & _
"on't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactio" & _
"ns=1"
'
'OleDbDataAdapter1
'
Me.OleDbDataAdapter1.DeleteCommand = Me.OleDbDeleteCommand1
Me.OleDbDataAdapter1.InsertCommand = Me.OleDbInsertCommand1
Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
Me.OleDbDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Nominee", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("Name", "Name"), New System.Data.Common.DataColumnMapping("NomId", "NomId"), New System.Data.Common.DataColumnMapping("Photo", "Photo"), New System.Data.Common.DataColumnMapping("Signature", "Signature")})})
Me.OleDbDataAdapter1.UpdateCommand = Me.OleDbUpdateCommand1
'
'OleDbSelectCommand1
'
Me.OleDbSelectCommand1.CommandText = "SELECT Name, NomId, Photo, Signature FROM Nominee"
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
'
'OleDbInsertCommand1
'
Me.OleDbInsertCommand1.CommandText = "INSERT INTO Nominee(Name, NomId, Photo, Signature) VALUES (?, ?, ?, ?)"
Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Name", System.Data.OleDb.OleDbType.VarWChar, 50, "Name"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("NomId", System.Data.OleDb.OleDbType.VarWChar, 50, "NomId"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Photo", System.Data.OleDb.OleDbType.VarBinary, 0, "Photo"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Signature", System.Data.OleDb.OleDbType.VarWChar, 50, "Signature"))
'
'OleDbUpdateCommand1
'
Me.OleDbUpdateCommand1.CommandText = "UPDATE Nominee SET Name = ?, NomId = ?, Photo = ?, Signature = ? WHERE (NomId = ?" & _
") AND (Name = ? OR ? IS NULL AND Name IS NULL) AND (Signature = ? OR ? IS NULL A" & _
"ND Signature IS NULL)"
Me.OleDbUpdateCommand1.Connection = Me.OleDbConnection1
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Name", System.Data.OleDb.OleDbType.VarWChar, 50, "Name"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("NomId", System.Data.OleDb.OleDbType.VarWChar, 50, "NomId"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Photo", System.Data.OleDb.OleDbType.VarBinary, 0, "Photo"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Signature", System.Data.OleDb.OleDbType.VarWChar, 50, "Signature"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_NomId", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "NomId", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Name", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Name", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Name1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Name", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Signature", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Signature", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Signature1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Signature", System.Data.DataRowVersion.Original, Nothing))
'
'OleDbDeleteCommand1
'
Me.OleDbDeleteCommand1.CommandText = "DELETE FROM Nominee WHERE (NomId = ?) AND (Name = ? OR ? IS NULL AND Name IS NULL" & _
") AND (Signature = ? OR ? IS NULL AND Signature IS NULL)"
Me.OleDbDeleteCommand1.Connection = Me.OleDbConnection1
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_NomId", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "NomId", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Name", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Name", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Name1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Name", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Signature", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Signature", System.Data.DataRowVersion.Original, Nothing))
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Signature1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Signature", System.Data.DataRowVersion.Original, Nothing))
'
'DataSet11
'
Me.DataSet11.DataSetName = "DataSet1"
Me.DataSet11.Locale = New System.Globalization.CultureInfo("en-US")
'
'Form4
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 438)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form4"
Me.Text = "Form4"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(DataSet11, "Nominee")
DataGrid1.DataSource = DataSet11.Tables("Nominee")
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Nominee"
DataSet11.Tables("Nominee").Columns.Add("Button")
' since the dataset has things like field name and number of columns,
' we will use those to create new columnstyles for the columns in our DB table
Dim numCols As Integer = DataSet11.Tables("Nominee").Columns.Count
Dim buttonColStyle As DataGridButtonColumn = Nothing
For i As Integer = 0 To numCols - 1
If DataSet11.Tables("Nominee").Columns(i).ColumnName.Equals("Button") Then
buttonColStyle = New DataGridButtonColumn(i)
'pass the column#
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(tableStyle)
End Sub
Private Sub HandleCellButtonClick(ByVal sender As Object, ByVal e As DataGridButtonColumn.DataGridCellButtonClickEventArgs)
MessageBox.Show("row " + e.RowIndex.ToString() + " Click Success")
End Sub
Public Class DataGridButtonColumn
Inherits DataGridTextBoxColumn
Public Event CellButtonClicked As DataGridCellButtonClickEventHandler
Private _button As Bitmap
Private _buttonPressed As Bitmap
Private _columnNum As Integer
Private _pressedRow As Integer
Public Sub New(ByVal colNum As Integer)
_columnNum = colNum
_pressedRow = -1
Try
Dim c As Char() = {","c}
Dim strm As System.IO.Stream = Me.[GetType]().Assembly.GetManifestResourceStream(Me.[GetType]().Assembly.ToString().Split(c, 10)(0) + ".button.bmp")
_button = New Bitmap(strm)
strm = Me.[GetType]().Assembly.GetManifestResourceStream(Me.[GetType]().Assembly.ToString().Split(c, 10)(0) + ".buttonpressed.bmp")
_buttonPressed = New Bitmap(strm)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Protected Overrides Sub Abort(ByVal rowNum As Integer)
End Sub
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
End Function
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
End Sub
Private Sub DrawButton(ByVal g As Graphics, ByVal bm As Bitmap, ByVal bounds As Rectangle, ByVal row As Integer)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim s As String = dg(row, Me._columnNum).ToString()
Dim sz As SizeF = g.MeasureString(s, dg.Font, bounds.Width - 4, StringFormat.GenericTypographic)
Dim x As Integer = bounds.Left + Math.Max(0, (bounds.Width - CInt(sz.Width)) / 2)
g.DrawImage(bm, bounds, 0, 0, bm.Width, bm.Height, _
GraphicsUnit.Pixel)
If sz.Height < bounds.Height Then
Dim y As Integer = bounds.Top + (bounds.Height - CInt(sz.Height)) / 2
If _buttonPressed Is bm Then
x += 1
End If
g.DrawString(s, dg.Font, New SolidBrush(dg.ForeColor), x, y)
End If
End Sub
Public Sub HandleMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = (hti.Column = Me._columnNum AndAlso hti.Row > -1)
_pressedRow = -1
Dim rect As New Rectangle(0, 0, 0, 0)
If isClickInCell Then
rect = dg.GetCellBounds(hti.Row, hti.Column)
isClickInCell = (e.X > rect.Right - Me._button.Width)
End If
If isClickInCell Then
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
' g.DrawImage(this._button, rect.Right - this._button.Width, rect.Y);
DrawButton(g, Me._button, rect, hti.Row)
g.Dispose()
RaiseEvent CellButtonClicked(Me, New DataGridCellButtonClickEventArgs(hti.Row, hti.Column))
End If
End Sub
Public Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = (hti.Column = Me._columnNum AndAlso hti.Row > -1)
Dim rect As New Rectangle(0, 0, 0, 0)
If isClickInCell Then
rect = dg.GetCellBounds(hti.Row, hti.Column)
isClickInCell = (e.X > rect.Right - Me._button.Width)
End If
If isClickInCell Then
'Console.WriteLine("HandleMouseDown " + hti.Row.ToString());
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
'g.DrawImage(this._buttonPressed, rect.Right - this._buttonPressed.Width, rect.Y);
DrawButton(g, _buttonPressed, rect, hti.Row)
g.Dispose()
_pressedRow = hti.Row
End If
End Sub
Protected Overrides Function GetMinimumHeight() As Integer
End Function
Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
End Function
Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size
End Function
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
Dim parent As DataGrid = Me.DataGridTableStyle.DataGrid
Dim current As Boolean = parent.IsSelected(rowNum) OrElse (parent.CurrentRowIndex = rowNum AndAlso parent.CurrentCell.ColumnNumber = Me._columnNum)
'draw the button
Dim bm As Bitmap = IIf(_pressedRow = rowNum, Me._buttonPressed, Me._button)
Me.DrawButton(g, bm, bounds, rowNum)
'font.Dispose();
End Sub
Public Delegate Sub DataGridCellButtonClickEventHandler(ByVal sender As Object, ByVal e As DataGridCellButtonClickEventArgs)
Public Class DataGridCellButtonClickEventArgs
Inherits EventArgs
Private _row As Integer
Private _col As Integer
Public Sub New(ByVal row As Integer, ByVal col As Integer)
_row = row
_col = col
End Sub
Public ReadOnly Property RowIndex() As Integer
Get
Return _row
End Get
End Property
Public ReadOnly Property ColIndex() As Integer
Get
Return _col
End Get
End Property
End Class
End Class
It is running but no button is not displaying.It is null there.
Pls help me out.
ANSWER: appologies for the in-convinence please send me the whole project zipped to: syedrizwanm at yahoo dot com
---------- FOLLOW-UP ----------
QUESTION: Hi
Is it possible to make the imagecolumn in datagrid to zoom in and zoom out when the mouse is over the image.
Pls give me your suggestions or code for this.
Answer For this you will have to write some funky javascript to show a bigger image on mouseover and thumbnail image on mouseout event.
For more information either post in javascript category or search on google. Once you get the javascript you can come back and ask about how to attach it to the grid.