DataGridView - question/help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miro

    DataGridView - question/help

    My current headache is proper is with the datagridview

    I am starting to realize that a DataGridView within vs2008 is not as
    'robust' as a 'textboxfield' by default for example.

    Example: A Textbox field can have masking, you can add easy validation and
    so on.

    Just adding a dummy datagridview to a form, databounding it, and allowing
    editing works - but is not dummy proof.
    For example: an integer bound to a column:
    there is no way offhand to add a 'editmask' to the column, so right now I
    must do this in the "CellValidating " event.
    ===
    IF mydgv.Columns(e .ColumnIndex).N ame = "txtANumber " Then
    If Not IsNumeric(e.For mattedValue) Then
    If e.FormattedValu e.ToString = "" Then
    e.Cancel = False
    Else
    e.Cancel = True
    End If
    Else
    If e.FormattedValu e.ToString <CInt(e.Formatt edValue).ToStri ng Then
    e.Cancel = True
    End If
    End If
    End If
    ===
    and do this for every field in there that is numeric and I expect just an
    integer.
    Otherwise, a user can type in ABC into the field, or a user can type in a
    number as 1.1.1 and it will get an exception error.

    I am looking for some good documentation - step by step somehwere as to how
    to add parts to a datagrid view like an editmask or something. I am not
    trying to create a 'complex' datagridview', just want to somehow properly
    validate data.

    Or am I missing something about validating data on a datagrid view. *** how
    to properly errorcheck each field

    One other thing I have found that is if I create a combobox on a field, the
    list within the combobox must be the value in the field.
    So I can't seem to create a textbox so a user can type in whatever they
    want, and on the dropdown, let them pick from standard answers. The fill of
    the dgv fails. -But thats a different story, I first have to learn how to
    error handle :)

    Thanks,

    Miro

  • Rich P

    #2
    Re: DataGridView - question/help

    Here is a way of thinking that might be of some value.

    I have read various complaints about .Net that it does not have
    continuous forms like MS Access, and other stuff that is pre built in.
    Yes, this is so because .Net is supposed to be an object oriented
    programming environment composed of various languages like C#, VB.net...

    ..Net is kind of low level but not as low as MFC. .Net does come with
    some pre-built controls, but they are low level (MFC comes with even
    less stuff - way lower level). You have to program the rest yourself
    which gives you way more flexibility and robustness than you would get
    with something that is already built-in.

    If you are stuck on a specific thing in your
    programming you could ask "how do you make a datagridview do something?"
    or "Can a datagridview do this - how to do it?

    For restricting data entry in specific fields in a datagridview you can
    add a delegate - something like

    For each cel as DatagridviewCel l In Datagridview1
    AddHandler cel.CellValueCh anged, AddressOf yourcustomeSub
    Next

    In yourCustomSub you specify the limitations of each cell. In pre-built
    software this stuff is done for you - the same way except that you can't
    customize it as much.

    Rich

    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Miro

      #3
      Re: DataGridView - question/help

      Yes I expected it that I can inherit from the datagridview and add in my
      one -and it makes it more flexible.

      I will google up datagridview and delegates / error checking or somethign
      like that.

      Currently im just trying to see how to properly validate data - and also
      thought if I was to add an edit mask on the file, that would potentially
      solve my 'validation' process for me. ( or at least in theory )

      Thanks

      Miro


      "Rich P" <rpng123@aol.co mwrote in message
      news:e5txAcQFJH A.3408@TK2MSFTN GP04.phx.gbl...
      Here is a way of thinking that might be of some value.
      >
      I have read various complaints about .Net that it does not have
      continuous forms like MS Access, and other stuff that is pre built in.
      Yes, this is so because .Net is supposed to be an object oriented
      programming environment composed of various languages like C#, VB.net...
      >
      Net is kind of low level but not as low as MFC. .Net does come with
      some pre-built controls, but they are low level (MFC comes with even
      less stuff - way lower level). You have to program the rest yourself
      which gives you way more flexibility and robustness than you would get
      with something that is already built-in.
      >
      If you are stuck on a specific thing in your
      programming you could ask "how do you make a datagridview do something?"
      or "Can a datagridview do this - how to do it?
      >
      For restricting data entry in specific fields in a datagridview you can
      add a delegate - something like
      >
      For each cel as DatagridviewCel l In Datagridview1
      AddHandler cel.CellValueCh anged, AddressOf yourcustomeSub
      Next
      >
      In yourCustomSub you specify the limitations of each cell. In pre-built
      software this stuff is done for you - the same way except that you can't
      customize it as much.
      >
      Rich
      >
      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Rich P

        #4
        Re: DataGridView - question/help

        The Datagridview Event that I find the most useful for data validation
        is the CellValueChange d event. I was thinking delegates, but just tried
        it - wrong way. What you do in this event is note the column Name using

        datagridview1.r ows(e.RowIndex) .Cells(e.Column Index).Name

        If this name.Equals("fl d1") then
        '--add your restrictions here
        ...

        you could probably use a Select Case structure

        strName = dg.Rows(e.RowIn dex).Cells(e.Co lumnIndex).Name
        Select Case strName
        Case "fld1"
        '--do something
        Case "fld2"
        '--do something
        ...
        End Select

        Rich

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Miro

          #5
          Re: DataGridView - question/help

          "Rich P" <rpng123@aol.co mwrote in message
          news:uxEu4gSFJH A.2292@TK2MSFTN GP04.phx.gbl...
          The Datagridview Event that I find the most useful for data validation
          is the CellValueChange d event. I was thinking delegates, but just tried
          it - wrong way. What you do in this event is note the column Name using
          >
          datagridview1.r ows(e.RowIndex) .Cells(e.Column Index).Name
          >
          If this name.Equals("fl d1") then
          '--add your restrictions here
          ..
          >
          you could probably use a Select Case structure
          >
          strName = dg.Rows(e.RowIn dex).Cells(e.Co lumnIndex).Name
          Select Case strName
          Case "fld1"
          '--do something
          Case "fld2"
          '--do something
          ..
          End Select
          >
          Rich
          >
          *** Sent via Developersdex http://www.developersdex.com ***


          I do not think CellValueChange d will work
          CellValueChange d fires after the CellValidating, so putting the number 1.1
          into a numeric field that is bound to an integer field in a database causes
          a
          "DataGridVi ew Default Error Dialog"
          "System.FormatE xception: Value was either too large or too small for an
          Int16. --- ( and so on )

          currenty im googling for the System.FormatEx ception to see how others solve
          the issue.

          Miro


          Comment

          • Miro

            #6
            Re: DataGridView - question/help

            I think I got it 1/2 way.

            I used the DataError event on the datagrid view.

            If TypeOf e.Exception Is System.FormatEx ception Then
            e.Cancel = True
            End If






            "Miro" <miro@beero.com wrote in message
            news:eHK8tFTFJH A.4064@TK2MSFTN GP05.phx.gbl...
            "Rich P" <rpng123@aol.co mwrote in message
            news:uxEu4gSFJH A.2292@TK2MSFTN GP04.phx.gbl...
            >The Datagridview Event that I find the most useful for data validation
            >is the CellValueChange d event. I was thinking delegates, but just tried
            >it - wrong way. What you do in this event is note the column Name using
            >>
            >datagridview1. rows(e.RowIndex ).Cells(e.Colum nIndex).Name
            >>
            >If this name.Equals("fl d1") then
            >'--add your restrictions here
            >..
            >>
            >you could probably use a Select Case structure
            >>
            >strName = dg.Rows(e.RowIn dex).Cells(e.Co lumnIndex).Name
            >Select Case strName
            > Case "fld1"
            > '--do something
            > Case "fld2"
            > '--do something
            >..
            >End Select
            >>
            >Rich
            >>
            >*** Sent via Developersdex http://www.developersdex.com ***
            >
            >
            >
            I do not think CellValueChange d will work
            CellValueChange d fires after the CellValidating, so putting the number 1.1
            into a numeric field that is bound to an integer field in a database
            causes a
            "DataGridVi ew Default Error Dialog"
            "System.FormatE xception: Value was either too large or too small for an
            Int16. --- ( and so on )
            >
            currenty im googling for the System.FormatEx ception to see how others
            solve the issue.
            >
            Miro
            >
            >

            Comment

            • Miro

              #7
              Re: DataGridView - question/help

              Sorry for the double post - I modified the code even more in the DataError
              event:
              This seems to work better

              Try
              Throw e.Exception
              Catch ex As System.FormatEx ception
              'message or whatever
              Catch ex As Exception
              'message or whatever
              Finally
              'cancel the datainput
              e.Cancel = True
              End Try



              "Miro" <miro@beero.com wrote in message
              news:e0jefQTFJH A.1456@TK2MSFTN GP03.phx.gbl...
              >I think I got it 1/2 way.
              >
              I used the DataError event on the datagrid view.
              >
              If TypeOf e.Exception Is System.FormatEx ception Then
              e.Cancel = True
              End If
              >
              >
              >
              >
              >
              >
              "Miro" <miro@beero.com wrote in message
              news:eHK8tFTFJH A.4064@TK2MSFTN GP05.phx.gbl...
              >"Rich P" <rpng123@aol.co mwrote in message
              >news:uxEu4gSFJ HA.2292@TK2MSFT NGP04.phx.gbl.. .
              >>The Datagridview Event that I find the most useful for data validation
              >>is the CellValueChange d event. I was thinking delegates, but just tried
              >>it - wrong way. What you do in this event is note the column Name using
              >>>
              >>datagridview1 .rows(e.RowInde x).Cells(e.Colu mnIndex).Name
              >>>
              >>If this name.Equals("fl d1") then
              >>'--add your restrictions here
              >>..
              >>>
              >>you could probably use a Select Case structure
              >>>
              >>strName = dg.Rows(e.RowIn dex).Cells(e.Co lumnIndex).Name
              >>Select Case strName
              >> Case "fld1"
              >> '--do something
              >> Case "fld2"
              >> '--do something
              >>..
              >>End Select
              >>>
              >>Rich
              >>>
              >>*** Sent via Developersdex http://www.developersdex.com ***
              >>
              >>
              >>
              >I do not think CellValueChange d will work
              >CellValueChang ed fires after the CellValidating, so putting the number
              >1.1 into a numeric field that is bound to an integer field in a database
              >causes a
              >"DataGridVie w Default Error Dialog"
              >"System.Format Exception: Value was either too large or too small for an
              >Int16. --- ( and so on )
              >>
              >currenty im googling for the System.FormatEx ception to see how others
              >solve the issue.
              >>
              >Miro
              >>
              >>
              >

              Comment

              • Miro

                #8
                Re: DataGridView - question/help

                One more post - I found a simple solution on how to add an editmask column
                to a datagrid view.
                Thus making only numerics valid in the field.

                here is the link:


                I dont understand it - but it works!!!!

                There is no way I could have written it at my level currently - but
                hopefully in some time.

                Cheers'
                Miro

                "Miro" <miro@beero.com wrote in message
                news:%23$z4jVTF JHA.1456@TK2MSF TNGP03.phx.gbl. ..
                Sorry for the double post - I modified the code even more in the DataError
                event:
                This seems to work better
                >
                Try
                Throw e.Exception
                Catch ex As System.FormatEx ception
                'message or whatever
                Catch ex As Exception
                'message or whatever
                Finally
                'cancel the datainput
                e.Cancel = True
                End Try
                >
                >
                >
                "Miro" <miro@beero.com wrote in message
                news:e0jefQTFJH A.1456@TK2MSFTN GP03.phx.gbl...
                >>I think I got it 1/2 way.
                >>
                >I used the DataError event on the datagrid view.
                >>
                > If TypeOf e.Exception Is System.FormatEx ception Then
                > e.Cancel = True
                > End If
                >>
                >>
                >>
                >>
                >>
                >>
                >"Miro" <miro@beero.com wrote in message
                >news:eHK8tFTFJ HA.4064@TK2MSFT NGP05.phx.gbl.. .
                >>"Rich P" <rpng123@aol.co mwrote in message
                >>news:uxEu4gSF JHA.2292@TK2MSF TNGP04.phx.gbl. ..
                >>>The Datagridview Event that I find the most useful for data validation
                >>>is the CellValueChange d event. I was thinking delegates, but just
                >>>tried
                >>>it - wrong way. What you do in this event is note the column Name using
                >>>>
                >>>datagridview 1.rows(e.RowInd ex).Cells(e.Col umnIndex).Name
                >>>>
                >>>If this name.Equals("fl d1") then
                >>>'--add your restrictions here
                >>>..
                >>>>
                >>>you could probably use a Select Case structure
                >>>>
                >>>strName = dg.Rows(e.RowIn dex).Cells(e.Co lumnIndex).Name
                >>>Select Case strName
                >>> Case "fld1"
                >>> '--do something
                >>> Case "fld2"
                >>> '--do something
                >>>..
                >>>End Select
                >>>>
                >>>Rich
                >>>>
                >>>*** Sent via Developersdex http://www.developersdex.com ***
                >>>
                >>>
                >>>
                >>I do not think CellValueChange d will work
                >>CellValueChan ged fires after the CellValidating, so putting the number
                >>1.1 into a numeric field that is bound to an integer field in a database
                >>causes a
                >>"DataGridVi ew Default Error Dialog"
                >>"System.Forma tException: Value was either too large or too small for an
                >>Int16. --- ( and so on )
                >>>
                >>currenty im googling for the System.FormatEx ception to see how others
                >>solve the issue.
                >>>
                >>Miro
                >>>
                >>>
                >>
                >

                Comment

                • Cor Ligthert[MVP]

                  #9
                  Re: DataGridView - question/help

                  Miro,

                  Maybe will this help you, there is more about the datagridview on our pages.



                  Cor

                  "Miro" <miro@beero.com schreef in bericht
                  news:eZbpfYOFJH A.5224@TK2MSFTN GP03.phx.gbl...
                  My current headache is proper is with the datagridview
                  >
                  I am starting to realize that a DataGridView within vs2008 is not as
                  'robust' as a 'textboxfield' by default for example.
                  >
                  Example: A Textbox field can have masking, you can add easy validation
                  and so on.
                  >
                  Just adding a dummy datagridview to a form, databounding it, and allowing
                  editing works - but is not dummy proof.
                  For example: an integer bound to a column:
                  there is no way offhand to add a 'editmask' to the column, so right now I
                  must do this in the "CellValidating " event.
                  ===
                  IF mydgv.Columns(e .ColumnIndex).N ame = "txtANumber " Then
                  If Not IsNumeric(e.For mattedValue) Then
                  If e.FormattedValu e.ToString = "" Then
                  e.Cancel = False
                  Else
                  e.Cancel = True
                  End If
                  Else
                  If e.FormattedValu e.ToString <CInt(e.Formatt edValue).ToStri ng
                  Then
                  e.Cancel = True
                  End If
                  End If
                  End If
                  ===
                  and do this for every field in there that is numeric and I expect just an
                  integer.
                  Otherwise, a user can type in ABC into the field, or a user can type in a
                  number as 1.1.1 and it will get an exception error.
                  >
                  I am looking for some good documentation - step by step somehwere as to
                  how to add parts to a datagrid view like an editmask or something. I am
                  not trying to create a 'complex' datagridview', just want to somehow
                  properly validate data.
                  >
                  Or am I missing something about validating data on a datagrid view. ***
                  how to properly errorcheck each field
                  >
                  One other thing I have found that is if I create a combobox on a field,
                  the list within the combobox must be the value in the field.
                  So I can't seem to create a textbox so a user can type in whatever they
                  want, and on the dropdown, let them pick from standard answers. The fill
                  of the dgv fails. -But thats a different story, I first have to learn how
                  to error handle :)
                  >
                  Thanks,
                  >
                  Miro

                  Comment

                  • Miro

                    #10
                    Re: DataGridView - question/help

                    Yes,
                    I see your example on page 5
                    DataGridView: Masked Edit Column

                    I must have missed it somehow. I do have your page in my quick links and
                    took a peek there first.

                    I think the making of custom classes with 'inheritance' is still a bit out
                    of my league.

                    Thank You Cor,

                    Miro

                    "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                    news:5E3D55F7-81AB-4D2D-8301-5014BF4F69C9@mi crosoft.com...
                    Miro,
                    >
                    Maybe will this help you, there is more about the datagridview on our
                    pages.
                    >

                    >
                    Cor
                    >
                    "Miro" <miro@beero.com schreef in bericht
                    news:eZbpfYOFJH A.5224@TK2MSFTN GP03.phx.gbl...
                    >My current headache is proper is with the datagridview
                    >>
                    >I am starting to realize that a DataGridView within vs2008 is not as
                    >'robust' as a 'textboxfield' by default for example.
                    >>
                    >Example: A Textbox field can have masking, you can add easy validation
                    >and so on.
                    >>
                    >Just adding a dummy datagridview to a form, databounding it, and allowing
                    >editing works - but is not dummy proof.
                    >For example: an integer bound to a column:
                    >there is no way offhand to add a 'editmask' to the column, so right now I
                    >must do this in the "CellValidating " event.
                    >===
                    >IF mydgv.Columns(e .ColumnIndex).N ame = "txtANumber " Then
                    > If Not IsNumeric(e.For mattedValue) Then
                    > If e.FormattedValu e.ToString = "" Then
                    > e.Cancel = False
                    > Else
                    > e.Cancel = True
                    > End If
                    > Else
                    > If e.FormattedValu e.ToString <CInt(e.Formatt edValue).ToStri ng
                    >Then
                    > e.Cancel = True
                    > End If
                    > End If
                    >End If
                    >===
                    >and do this for every field in there that is numeric and I expect just an
                    >integer.
                    >Otherwise, a user can type in ABC into the field, or a user can type in a
                    >number as 1.1.1 and it will get an exception error.
                    >>
                    >I am looking for some good documentation - step by step somehwere as to
                    >how to add parts to a datagrid view like an editmask or something. I am
                    >not trying to create a 'complex' datagridview', just want to somehow
                    >properly validate data.
                    >>
                    >Or am I missing something about validating data on a datagrid view. ***
                    >how to properly errorcheck each field
                    >>
                    >One other thing I have found that is if I create a combobox on a field,
                    >the list within the combobox must be the value in the field.
                    >So I can't seem to create a textbox so a user can type in whatever they
                    >want, and on the dropdown, let them pick from standard answers. The fill
                    >of the dgv fails. -But thats a different story, I first have to learn
                    >how to error handle :)
                    >>
                    >Thanks,
                    >>
                    >Miro
                    >

                    Comment

                    Working...