473,573 Members | 2,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Sep 12 '08 #1
9 3410
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 ***
Sep 12 '08 #2
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.co mwrote in message
news:e5******** ******@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 ***
Sep 12 '08 #3
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 ***
Sep 12 '08 #4
"Rich P" <rp*****@aol.co mwrote in message
news:ux******** ******@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
Sep 12 '08 #5
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" <mi**@beero.com wrote in message
news:eH******** ******@TK2MSFTN GP05.phx.gbl...
"Rich P" <rp*****@aol.co mwrote in message
news:ux******** ******@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

Sep 13 '08 #6
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" <mi**@beero.com wrote in message
news:e0******** ******@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" <mi**@beero.com wrote in message
news:eH******** ******@TK2MSFTN GP05.phx.gbl...
>"Rich P" <rp*****@aol.co mwrote in message
news:ux******* *******@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

Sep 13 '08 #7
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.com wrote in message
news:%2******** ********@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" <mi**@beero.com wrote in message
news:e0******** ******@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" <mi**@beero.com wrote in message
news:eH******* *******@TK2MSFT NGP05.phx.gbl.. .
>>"Rich P" <rp*****@aol.co mwrote in message
news:ux****** ********@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

Sep 13 '08 #8
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.com schreef in bericht
news:eZ******** ******@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
Sep 13 '08 #9
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******** *************** ***********@mic rosoft.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.com schreef in bericht
news:eZ******** ******@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
Sep 13 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6934
by: TNSFED | last post by:
I have a dilemma when trying to delete a row from the DataGridView. Here is a sample of my code: private void dgv_EQUPS_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { fDeleteRow = false; if (chx_DeleteFlag.Checked) {
1
3151
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private WithEvent DataTable as New DataTable
7
12583
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's the background for my question... Before I switched my application over to the Fx 2.0, I used a DataGrid to display my data. I would store...
2
7812
by: michael sorens | last post by:
I have been trying to figure out how to use DataSets, BindingSources, DataGridViews, and XML together, but it is a challenge. I understand how to populate a DataGridView with XML basically as: DataSet ds = new DataSet(); ds.ReadXml(@"\usr\tmp\sample.xml"); dataGridView.DataSource = ds; dataGridView.DataMember = "targetElement"; What I...
2
4340
by: MKBender | last post by:
I'm pretty new to .net, so your help is very apprciated. So I have a DataGridView that has a dataTable data source. This dataGridView is also editable. When a user tries to close an open data open or open a different data file, I would like to ask if the user wants to save his changes, but only if he made changes. I don't know how to detect...
3
6318
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
New to databinding in vs2005, I always did it manually in 2003. I have no problem loading comboboxes, and a change in that combobox changes the data in the textboxes but I can not figure out a way to get the data in a datagridview to change. For example 2 columns are ID and amount_paid, the datagridview loads on form load with all ID's and...
7
15636
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is there an equivalent property for the DataGridView? I have searched, but have not found one. I would like the user to be able to see all the columns...
11
76099
by: dave18 | last post by:
Hello all! I found a solution to my original question, but there's still so much I don't understand about it, I thought I'd give this forum a try. At the very least, maybe it will help someone else who got stumped like I did. It seems so simple... binding a DataGridView to a List<T>. These are the two general problems that I kept running...
6
3210
by: Miro | last post by:
Sorry for the cross post. I am stuck. I have a datagridview for poker rounds. Basically there are 3 columns in this datagridview. "Round" "SmallBlind" "BigBlind" I have an issue when I tab through the new row being added. It does not 'Add' that row, nor setup the 'next blank add row' so I can continue to tab
0
7694
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8021
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7787
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8065
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5593
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1303
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.