DataGridView - question/help


M

Miro

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
 
Ad

Advertisements

R

Rich P

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
 
M

Miro

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
 
R

Rich P

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
 
M

Miro

Rich P said:
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



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
 
M

Miro

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
 
Ad

Advertisements

M

Miro

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
 
Ad

Advertisements

M

Miro

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top