The application of the button column. (Add a button column to the DataGrid, WinForms)

Source: Internet
Author: User
Tags integer tostring
datagrid| Button Public Class mybuttondatagridtablestyle1
Inherits System.Windows.Forms.DataGridTextBoxColumn

#Region "code generated by the Windows forms Designer"

Public Sub New ()
MyBase.New ()

' This call is required by the Windows Forms Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' UserControl overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
components = New System.ComponentModel.Container ()
End Sub

#End Region
Public Delegate Sub Datagridcellbuttonclickeventhandler (ByVal sender as Object, ByVal e As Datagridcellbuttonclickeventargs)
Public Event cellbuttonclicked as Datagridcellbuttonclickeventhandler

Private M_face as Bitmap
Private m_facepressed as Bitmap
Private M_columnnum as Integer
Private M_row as Integer


Public Sub New (ByVal colnum as Integer)
M_columnnum = Colnum
M_row =-1

Try
Dim strm as System.IO.Stream = Me.GetType (). Assembly.GetManifestResourceStream ("Btnface.bmp")
M_face = New Bitmap (STRM)
STRM = Me.GetType (). Assembly.GetManifestResourceStream ("Btnpressed.bmp")
m_facepressed = New Bitmap (STRM)
Catch
End Try
End Sub

Protected Overloads Overrides Sub Edit (ByVal [source] as System.Windows.Forms.CurrencyManager, ByVal rownum as Integer, by Val bounds as System.Drawing.Rectangle, ByVal [readOnly] as Boolean, ByVal Instanttext as String, ByVal cellisvisible as B Oolean)

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

M_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.m_face.width
End If
If Isclickincell Then
Dim g as Graphics = Graphics.fromhwnd (DG. Handle)
G.drawimage (Me.m_face, rect. Right-me.m_face.width, Rect. Y
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.m_columnnum
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.m_face.width
End If

If Isclickincell Then

Dim g as Graphics = Graphics.fromhwnd (DG. Handle)
G.drawimage (me.m_facepressed, rect. Right-me.m_facepressed.width, Rect. Y
G.dispose ()
M_row = Hti. Row
End If
End Sub

' Redraw
Protected Overloads Overrides Sub Paint (ByVal g as System.Drawing.Graphics, ByVal bounds as System.Drawing.Rectangle, Byva l [Source] as System.Windows.Forms.CurrencyManager, ByVal rownum as Integer, ByVal Backbrush as System.Drawing.Brush, Byva L Forebrush as System.Drawing.Brush, ByVal aligntoright as Boolean)

Dim parent as DataGrid = Me.DataGridTableStyle.DataGrid
' If the row is the row number of the selected row or the current cell = the line number of the line being clicked and the column number of the current cell equals new The column number parameter
Dim Current as Boolean = parent. IsSelected (RowNum) Or (parent. Currentrowindex = rownum and parent. Currentcell.columnnumber = me.m_columnnum)

Dim BackColor as Color
If Current Then BackColor = parent. SelectionBackColor Else BackColor = parent. BackColor
Dim ForeColor as Color
If current Then ForeColor = parent. Selectionforecolor Else ForeColor = parent. ForeColor

' Please empty cell
G.fillrectangle (New SolidBrush (BackColor), bounds)

' Draw value
Dim s as String = Me.getcolumnvalueatrow ([source], rownum). ToString () ' Parent[rownum, 0]. ToString () + ((Parent[rownum, 1]. ToString ()) + ""). Substring (0,2);

g.DrawString (S, parent.) Font, New SolidBrush (ForeColor), bounds. X, bounds. Y


Dim BM as Bitmap
If M_row = rownum Then BM = me.m_facepressed Else BM = Me.m_face
G.drawimage (BM, bounds.) RIGHT-BM. Width, bounds. Y
End Sub
End Class


Calling code:

Private Function getdatagridstyle (ByVal table as DataTable) as DataGridTableStyle
Dim style as New DataGridTableStyle ()
Style. MappingName = table. TableName
Style. Rowheaderwidth = 15
Dim I as Integer
For i = 0 to table. Columns.count-1
If i = 1 Then
Dim Textbuttoncolstyle as New mybuttondatagridtablestyle1 (i) ' Pass the column#
Textbuttoncolstyle.headertext = table. Columns (i). ColumnName
Textbuttoncolstyle.mappingname = table. Columns (i). ColumnName

' Hookup our Cellbutton handler ...
AddHandler textbuttoncolstyle.cellbuttonclicked, AddressOf Handlecellbuttonclick

Style. Gridcolumnstyles.add (Textbuttoncolstyle)

' Hook the mouse handlers
AddHandler Datagrid1.mousedown, AddressOf Textbuttoncolstyle.handlemousedown
AddHandler Datagrid1.mouseup, AddressOf textbuttoncolstyle.handlemouseup
Else
Dim C as New DataGridTextBoxColumn ()
C.headertext = table. Columns (i). ColumnName
C.mappingname = table. Columns (i). ColumnName
Style. Gridcolumnstyles.add (c)
End If
Next
return style
End Function
Private Sub Handlecellbuttonclick (ByVal sender as Object, ByVal e as Datagridcellbuttonclickeventargs)
MessageBox.Show (("Row" + e.rowindex.tostring () + "col" + e.colindex.tostring () + "clicked."))
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.