Results 1 to 23 of 23

Thread: problem at host my custom control in datagridview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    problem at host my custom control in datagridview

    hi
    I crate a textbox control by Additional property as:

    InputType as inputs , inputs is enum :
    Public Enum Inputs
    Standard = 0 (user can enter anything)
    Numbers = 1 (only number)
    Alphabets = 2
    NumberAndAlphabet = 3
    Decimals = 4 (only decimal by 3 decimal place)
    NationalCode = 5 (my special country code for each person )
    End Enum

    Separator (if set to true , digit can be grouping)

    And some other properties


    this control(SHTextBox) Working properly , when I finish another component based on shtextbox (SHtextBoxDataGridViewColumn) based on following link information :

    Code:
    http://www.vb-tips.com/dbpages.aspx?ID=43d85ac7-1715-47fd-9e1f-dd17b41ace9a
    but when add my column to datagridview , I can not change the value of inputtype property.
    I read following article and see the note :

    Code:
    http://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.110).aspx
    Must I override the Clone method ? if yes how I do it ?

    I'm confused about host my control in datagridview and did not know what is the correct way

    I attach my project here , Please check the code and solve my problem.

    tank you
    Attached Files Attached Files
    Last edited by sh-a; May 6th, 2013 at 10:44 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: problem at host my custom control in datagridview

    As that page says, you have to override Clone in the derived cell and column classes IF you add properties. If you don't add properties then there's no additional properties to clone so the base implementation is fine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    my shtextbox control , have few properties for example , user can only enter numbers, I would like to know the properties of the previous definition in shtextbox , must be define again in shdatagridviewcolumn ? or when i inherit form shtextbox , all of last defined properties Available in new column ?

    please help me to solve my problem. if maybe correct my code please

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: problem at host my custom control in datagridview

    I can't open your project. What version of Visual Studio was this made in ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    vs 2012

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    sorry , in this class : SHDataGridViewTextBoxColumn
    replase this code at line 25

    Return cinputtype

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problem at host my custom control in datagridview

    Quote Originally Posted by jmcilhinney View Post
    As that page says, you have to override Clone in the derived cell and column classes IF you add properties. If you don't add properties then there's no additional properties to clone so the base implementation is fine.
    Well clearly in this case there are added properties and there is therefore a need to do it. Which leaves the second part of the question "if yes how I do it ?" unanswered. The MSDN article gives no help whatsoever (as usual!) and any attempt I've made results in Vb2012 crashing (not that that's difficult!) so, how do you do it?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: problem at host my custom control in datagridview

    Quote Originally Posted by dunfiddlin View Post
    Well clearly in this case there are added properties and there is therefore a need to do it. Which leaves the second part of the question "if yes how I do it ?" unanswered. The MSDN article gives no help whatsoever (as usual!) and any attempt I've made results in Vb2012 crashing (not that that's difficult!) so, how do you do it?
    This is the quote from that MSDN topic:
    When you derive from DataGridViewCell or DataGridViewColumn and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell or column.
    I would hope that it is clear from that that you need to do this:
    Code:
    Public Overrides Function Clone() As Object
        Dim copy = DirectCast(MyBase.Clone(), TheTypeOfThisClass)
    
        copy.MyAddedProperty = Me.MyAddedProperty
        'etc
    
        Return copy
    End Function
    The only time you need to override Clone in your column and cell classes is if you have added properties to your column and cell classes so all you need to do in those Clone methods is copy the values of the added properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    After a few days I solve my problem in hosting my custom control in datagridview. but there is another problem in cellvalidation event and Thousand Separator.
    keypress and textchanged events from my custom control (SHTextbox) work well in datagridview .if you change inputtype to number in SHDataGridViewTextBoxColumn , user can only input number in editing mode or separator work fine.

    but validation event not working!
    Of course i use the following article in form1 to validate all shtextboxes
    http://blog.scosby.com/post/2010/02/...ows-Forms.aspx

    but in datagridview , i want when user inpute unallowable entry , cellvalidating event run and preven move to another cell untill user modify and correct his entry. for example , numbers by length greater than 15 is not vlid in my program , or decimal valid length is 3
    . or negative symbol must be first character or illegal numbers is not allowed (.2123 or 1323.),

    how can set shtextbox validation event to SHDataGridViewTextBox ?

    another problem is , when user typing numbers in shtextbox , Thousand Separator run well but in SHDataGridViewTextBox only when control is in editing mode an user typing numbers , it working , and Upon move to other cell , Thousand Separator Disappears !

    pleae help me

    I don't know way I cant upload my project again here!
    the link from dropbox
    https://www.dropbox.com/s/7zm02hj44t...MyTextBox2.zip
    Last edited by sh-a; May 17th, 2013 at 03:59 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: problem at host my custom control in datagridview

    So, you're saying that you want to validate the data to ensure that it is a number and it doesn't exceed a certain precision and scale, correct? If so, here's a function that will do that:
    Code:
    Private Function ValidateNumber(text As String, precision As Integer, scale As Integer) As Boolean
        Dim number As Decimal
    
        Return Decimal.TryParse(text, number) AndAlso
               ValidatePrecision(number, precision) AndAlso
               ValidateScale(number, scale)
    End Function
    
    Private Function ValidatePrecision(number As Decimal, precision As Integer) As Boolean
        'Count the digits and check whether the result is within the prescribed precision.
        Return number.ToString().Select(Function(ch) Char.IsDigit(ch)).Count() <= precision
    End Function
    
    Private Function ValidateScale(number As Decimal, scale As Integer) As Boolean
        Dim text = number.ToString()
        Dim decimalSeparator = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
        Dim decimalSeparatorIndex = text.IndexOf(decimalSeparator)
    
        'If there is a decimal separator, count the digits after it and check whether the result is within the prescribed scale.
        Return decimalSeparatorIndex = -1 OrElse
               text.Skip(decimalSeparatorIndex + decimalSeparator.Length).Count() <= scale
    End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    hi
    i don't have porblem in validaing numbers ! number validating is in my shtextbox in textbox validating event. but when i host my control in datagridview , validation event don't work .
    my Question is why keypress and textchanged events (form shtextbox) work in datagridvew when editingcontrol showing but why validation event don't fired
    tank you

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    i attach new project , don't get it from first post
    Attached Files Attached Files

  13. #13
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problem at host my custom control in datagridview

    but when i host my control in datagridview , validation event don't work .
    I really don't understand what you mean. It seems to work perfectly. In Numbers mode only numbers are permitted so surely all the validation events are taking place as expected?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    in above project that attached, I change AutoValidate property of form1 to EnablePreventFocusChange. then I overrides onvalidating and onvalidated events at end of SHDataGridViewTextBoxEditingControl class
    now validation on my customcolumn work fine but when I click by mouse to move to another cell , validating event fired 1 time and prevent to move and when I press tab key or enter , validating fired 2 time and move to another cell whitout prevent!. how solve it to fire only one time by press tab key ?

    Code:
     Protected Overrides Sub OnValidating(e As CancelEventArgs)
    
                If AllowNull = False Then
                    If String.IsNullOrEmpty(Me.Text.Trim) = True Then
                        e.Cancel = True
                        ' show message that cell value can not be null
                        MessageBox.Show("لطفاً اطلاعات را وارد نمائید", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    End If
                    Select Case InputType
                        Case Inputs.NationalCode
                            If ValidateNcode(Me.Text.Trim) = False Then
                                e.Cancel = True
                                'show message that nationalcode is not in correct format
                                MessageBox.Show("لطفاً کد ملی را بصورت صحیح وارد نمائید", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            End If
                        Case Inputs.Numbers
                            If AllowNegative = True Then
                                If ismatched(Me.Text.Trim, negshregex) = False Then
                                    e.Cancel = True
                                    'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
                                    MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                End If
                            Else
                                If ismatched(Me.Text.Trim, shregex) = False Then
                                    e.Cancel = True
                                    'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
                                    MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                End If
                            End If
                        Case Inputs.Decimals
                            If AllowNegative = True Then
                                If ismatched(Me.Text.Trim, negshregex) = False Then
                                    e.Cancel = True
                                    'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
                                    MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                End If
                            Else
                                If ismatched(Me.Text.Trim, shregex) = False Then
                                    e.Cancel = True
                                    'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
                                    MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                End If
                            End If
                    End Select
                End If
                MyBase.OnValidating(e)
            End Sub
    
            Protected Overrides Sub OnValidated(e As EventArgs)
                MyBase.OnValidated(e)
            End Sub

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    when SHDataGridViewTextBox is in editing mode and I click by mouse on other cell , validating fired but when I press enter or tab my column exit from editing mode and validating don't work well. mean just show messagebox and don't prevent to move to other cells.
    I think we must capture enter or tab in editing mode an call validating , I don't know is this a way or not
    please help

  16. #16
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problem at host my custom control in datagridview

    Why would you want to prevent moving to another cell? Your users will press Tab or Enter specifically to do that very thing.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: problem at host my custom control in datagridview

    Quote Originally Posted by dunfiddlin View Post
    Why would you want to prevent moving to another cell? Your users will press Tab or Enter specifically to do that very thing.
    Because the user has to be prevented from committing invalid data so you have to decide whether you stop them at each invalid entry or pull them up on all invalid entires at the end. Generally speaking, I prefer the first option. It is perfectly legitimate to prevent the user moving on until they ensure that the current data is valid.

    @sh-a, I'm not sure that it will make any difference but, as a test at least, try calling the base OnValidating before performing your own validation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    dunfiddlin :
    Why would you want to prevent moving to another cell
    As I said above, the numbers should not enter more than 15 digits

    try calling the base OnValidating before performing your own validation.
    i edit oncellvalidating as following :
    Code:
       Protected Overrides Sub OnValidating(e As CancelEventArgs)
                MyBase.OnValidating(e)
                If AllowNull = False Then
    ..................
    but not working.

  19. #19
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problem at host my custom control in datagridview

    Because the user has to be prevented from committing invalid data
    Well normally yes, but surely the whole point of the custom editing control is to make it impossible to enter invalid data in the first place? The 15 digit maximum (or at least a MaximumDigits property) should be an inherent part of the custom control and therefore 'self' validating.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    I read something as following , is that can help to solve my problem ?

    http://kethare.in/2011/05/16/validat...idview-column/

    http://stackoverflow.com/questions/4...lidating-event

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    i debug program and found following information:

    when user typing in my custom column and then click on another cell or any where of form , this events fired :

    onlostfocus
    onleave
    onvalidating

    and validating work well

    when user typing in my custom column and then press ENTER, TAB or Esc , this events fired :

    onprivewkeydownn
    geteditingcontrolformattedvalue
    editingcontrolvaluechanged
    onlostfocus
    onleave
    onleave (shTextbox)
    onvalidating (my custom column)
    onvalidating (shtextbox)
    onvalidating (my custom column)

    by click enter or tab , esc , validating messagebox showing 1 time then my coustom column value ,Not shown , again messagebox showing , value showing in cell and without prevent , current cell changed
    why there is Difference between going to other by mouse VS keyboard ?
    i think i must capture (Tab , Enter , Esc) and call OnlostFocus and onleave or onvalidating
    but i don't know how i do it
    please help me
    Last edited by sh-a; May 20th, 2013 at 10:57 PM.

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    After a month, finally found what I needed at this link.

    http://www.aspnet-answers.com/micros...alidation.aspx

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem at host my custom control in datagridview

    if your datagridview loaded from database , you must edit InitializeEditingControl

    Code:
     Public Class SHDataGridViewTextBoxCell
            Inherits DataGridViewTextBoxCell
            
            Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)
                MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
                Dim shcol As SHDataGridViewTextBoxColumn = DirectCast(OwningColumn, SHDataGridViewTextBoxColumn)
                Dim shctl As SHDataGridViewTextBoxEditingControl = CType(DataGridView.EditingControl, SHDataGridViewTextBoxEditingControl)
                ' Because the datagridview fill from database , dosn't need set text
                'Try
                '    shctl.Text = Me.Value.ToString
                'Catch ex As Exception
                '    shctl.Text = ""
                'End Try
                shctl.InputType = shcol.InputType
                shctl.AllowNegative = shcol.AllowNegative
                shctl.AllowNull = shcol.AllowNull
                shctl.Separator = shcol.Separator
            End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width