Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i create a property named IsCheck (value true/false) in DataGridViewTextBoxColumn?

i searched the internet but cannot find a solution to create property. Please help me with a code snippet or so.
Posted
Comments
Richard MacCutchan 14-Feb-15 3:54am    
Why not just add a CheckBox column?
agent_kruger 14-Feb-15 4:01am    
no i need a property for coding purpose
Richard MacCutchan 14-Feb-15 4:37am    
How do you have a boolean property attached to a TextBox?
Richard MacCutchan 14-Feb-15 4:40am    
Have you checked to see whether any of the existing properties would help?
agent_kruger 14-Feb-15 6:21am    
no i need it for custom purpose

1 solution

I think you should overrided DataGridViewTextBoxColumn Like as bellow:

VB
Public Class sampleDataGridViewNumberTextBoxColumn
    Inherits System.Windows.Forms.DataGridViewTextBoxColumn
    //Define in here
EndClass


Pls refer this link[^]

This is sample code to customize GridViewTextboxColumn to GridViewNumberTextboxColumn:
VB
Option Explicit On
Option Strict On
 
Imports System.ComponentModel
Imports System.Windows.Forms
 
Public Class sampleDataGridViewNumberTextBoxColumn
    Inherits System.Windows.Forms.DataGridViewTextBoxColumn
 
    Private _orijinalText As String
    Private _minValue As Decimal
    Private _maxValue As Decimal
    Private _CellValueType As Type
 
    Sub New()
        InitializeComponent()
        Me.DefaultCellStyle.Format = "#,##0"
        Me._orijinalText = ""
        Me._minValue = -999999999
        Me._maxValue = 999999999
        Me._CellValueType = Type.Number
        SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        Dim cell As New sampleDataGridViewDateTextBoxCell()
        Me.CellTemplate = cell
    End Sub
    
    'This is override function
    Public Overrides Function Clone() As Object
        Dim cloneObject As sampleDataGridViewNumberTextBoxColumn = DirectCast(MyBase.Clone, sampleDataGridViewNumberTextBoxColumn)
        cloneObject.MinValue = Me.MinValue
        cloneObject.MaxValue = Me.MaxValue
        cloneObject.CellValueType = Me.CellValueType
        Return cloneObject
    End Function
    
    ''' <summary>
    ''' This is custom function to set max length
    ''' </summary>
    ''' <value></value>
    Private Sub SetMaxLength( _
        ByVal format As String, _
        ByVal minValue As Decimal, _
        ByVal maxValue As Decimal)
       
        Dim formattedMinValue As String = minValue.ToString(format)
        Dim formattedMaxValue As String = maxValue.ToString(format)
        
        If formattedMinValue.Length > formattedMaxValue.Length Then
            MyBase.MaxInputLength = formattedMinValue.Length
        Else
            MyBase.MaxInputLength = formattedMaxValue.Length
        End If
 
    End Sub
 

 
    ''' <summary>
    ''' This is custom property MinValue
    ''' </summary>
    ''' <value></value>
    <defaultvalue(gettype(decimal),> _
    Public Property MinValue() As Decimal
        Get
            Return _minValue
        End Get
        Set(ByVal value As Decimal)
            _minValue = value            
            SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        End Set
    End Property
 
    ''' <summary>
    ''' This is custom property MaxValue
    ''' </summary>
    ''' <value></value>
    <defaultvalue(gettype(decimal),> _
    Public Property MaxValue() As Decimal
        Get
            Return _maxValue
        End Get
        Set(ByVal value As Decimal)
            _maxValue = value            
            SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        End Set
    End Property
    
    'This is override function
    Public Shadows Property DefaultCellStyle() As DataGridViewCellStyle
        Get
            Return MyBase.DefaultCellStyle
        End Get
        Set(ByVal value As DataGridViewCellStyle)
            MyBase.DefaultCellStyle = value
            SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        End Set
    End Property
 

    ''' <summary>
    ''' MaxInputLength (This is override function)
    ''' </summary>
    <browsable(false)> _
    Public Shadows ReadOnly Property MaxInputLength() As Decimal
        Get
            Return MyBase.MaxInputLength
        End Get
    End Property
    
    Public Enum Type
        Number
        Code
    End Enum
 
End Class
 
Public Class sampleDataGridViewNumberTextBoxCell
    Inherits System.Windows.Forms.DataGridViewTextBoxCell
    'This is override function
    Public Overrides ReadOnly Property EditType() As System.Type
        Get
            Return GetType(sampleDataGridViewNumberTextBoxEditingControl)
        End Get
    End Property
 
End Class
 
Public Class sampleDataGridViewNumberTextBoxEditingControl
    Inherits System.Windows.Forms.DataGridViewTextBoxEditingControl
 
    Private WM_PASTE As Integer = &H302
    'This is override function
    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_PASTE Then
            Dim iData As IDataObject = Clipboard.GetDataObject()
            
            If iData.GetDataPresent(DataFormats.Text) Then
                Dim clipboardString As String = iData.GetData(DataFormats.Text).ToString
                'Acceped only number input
                If Not System.Text.RegularExpressions.Regex.IsMatch(clipboardString, "^\d+$") Then
                    Return
                End If
            End If
        End If
        
        MyBase.WndProc(m)
    End Sub
 

End Class


This code i read from internet.Hope this help .
 
Share this answer
 
v2
Comments
agent_kruger 14-Feb-15 4:04am    
i think the above code is in VB.net and though i tried that it did not work but still can you give me a sample code.
Thanh Xuan Vu 14-Feb-15 4:30am    
Yes this is vb.net code.
I'll post in answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900