By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,705 Members | 2,013 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

DataGridView - question/help

P: n/a
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).Name = "txtANumber" Then
If Not IsNumeric(e.FormattedValue) Then
If e.FormattedValue.ToString = "" Then
e.Cancel = False
Else
e.Cancel = True
End If
Else
If e.FormattedValue.ToString <CInt(e.FormattedValue).ToString 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

Sep 12 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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 DatagridviewCell In Datagridview1
AddHandler cel.CellValueChanged, 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 ***
Sep 12 '08 #2

P: n/a
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" <rp*****@aol.comwrote in message
news:e5**************@TK2MSFTNGP04.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 DatagridviewCell In Datagridview1
AddHandler cel.CellValueChanged, 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 ***
Sep 12 '08 #3

P: n/a
The Datagridview Event that I find the most useful for data validation
is the CellValueChanged 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.ColumnIndex ).Name

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

you could probably use a Select Case structure

strName = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).Name
Select Case strName
Case "fld1"
'--do something
Case "fld2"
'--do something
...
End Select

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 12 '08 #4

P: n/a
"Rich P" <rp*****@aol.comwrote in message
news:ux**************@TK2MSFTNGP04.phx.gbl...
The Datagridview Event that I find the most useful for data validation
is the CellValueChanged 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.ColumnIndex ).Name

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

you could probably use a Select Case structure

strName = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).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 CellValueChanged will work
CellValueChanged 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
"DataGridView Default Error Dialog"
"System.FormatException: Value was either too large or too small for an
Int16. --- ( and so on )

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

Miro
Sep 12 '08 #5

P: n/a
I think I got it 1/2 way.

I used the DataError event on the datagrid view.

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


"Miro" <mi**@beero.comwrote in message
news:eH**************@TK2MSFTNGP05.phx.gbl...
"Rich P" <rp*****@aol.comwrote in message
news:ux**************@TK2MSFTNGP04.phx.gbl...
>The Datagridview Event that I find the most useful for data validation
is the CellValueChanged 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.ColumnInde x).Name

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

you could probably use a Select Case structure

strName = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).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 CellValueChanged will work
CellValueChanged 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
"DataGridView Default Error Dialog"
"System.FormatException: Value was either too large or too small for an
Int16. --- ( and so on )

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

Miro

Sep 13 '08 #6

P: n/a
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.FormatException
'message or whatever
Catch ex As Exception
'message or whatever
Finally
'cancel the datainput
e.Cancel = True
End Try

"Miro" <mi**@beero.comwrote in message
news:e0**************@TK2MSFTNGP03.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.FormatException Then
e.Cancel = True
End If


"Miro" <mi**@beero.comwrote in message
news:eH**************@TK2MSFTNGP05.phx.gbl...
>"Rich P" <rp*****@aol.comwrote in message
news:ux**************@TK2MSFTNGP04.phx.gbl...
>>The Datagridview Event that I find the most useful for data validation
is the CellValueChanged 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.ColumnInd ex).Name

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

you could probably use a Select Case structure

strName = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).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 CellValueChanged will work
CellValueChanged 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
"DataGridView Default Error Dialog"
"System.FormatException: Value was either too large or too small for an
Int16. --- ( and so on )

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

Miro

Sep 13 '08 #7

P: n/a
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:
http://austincodecamp08.googlecode.c...d%20Tricks.pdf

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" <mi**@beero.comwrote in message
news:%2****************@TK2MSFTNGP03.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.FormatException
'message or whatever
Catch ex As Exception
'message or whatever
Finally
'cancel the datainput
e.Cancel = True
End Try

"Miro" <mi**@beero.comwrote in message
news:e0**************@TK2MSFTNGP03.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.FormatException Then
e.Cancel = True
End If


"Miro" <mi**@beero.comwrote in message
news:eH**************@TK2MSFTNGP05.phx.gbl...
>>"Rich P" <rp*****@aol.comwrote in message
news:ux**************@TK2MSFTNGP04.phx.gbl...
The Datagridview Event that I find the most useful for data validation
is the CellValueChanged 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.ColumnIn dex).Name

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

you could probably use a Select Case structure

strName = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).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 CellValueChanged will work
CellValueChanged 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
"DataGridView Default Error Dialog"
"System.FormatException: Value was either too large or too small for an
Int16. --- ( and so on )

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

Miro

Sep 13 '08 #8

P: n/a
Miro,

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

http://www.vb-tips.com/DataGridViewSample.aspx

Cor

"Miro" <mi**@beero.comschreef in bericht
news:eZ**************@TK2MSFTNGP03.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).Name = "txtANumber" Then
If Not IsNumeric(e.FormattedValue) Then
If e.FormattedValue.ToString = "" Then
e.Cancel = False
Else
e.Cancel = True
End If
Else
If e.FormattedValue.ToString <CInt(e.FormattedValue).ToString
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
Sep 13 '08 #9

P: n/a
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]" <no************@planet.nlwrote in message
news:5E**********************************@microsof t.com...
Miro,

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

http://www.vb-tips.com/DataGridViewSample.aspx

Cor

"Miro" <mi**@beero.comschreef in bericht
news:eZ**************@TK2MSFTNGP03.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).Name = "txtANumber" Then
If Not IsNumeric(e.FormattedValue) Then
If e.FormattedValue.ToString = "" Then
e.Cancel = False
Else
e.Cancel = True
End If
Else
If e.FormattedValue.ToString <CInt(e.FormattedValue).ToString
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
Sep 13 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.