473,756 Members | 3,111 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 3422
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
6949
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
3162
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
12630
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 different DataGridTableStyles (each one with a custom set of columns) in the DataGrid.TableStyles property...
2
7822
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 found through experimentation is that the DataMember may specify
2
4354
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 if the user made changes to the datagridview Is there a simple property part of the datagridview...
3
6330
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 amounts. How do I get it to only bring back the selected ID. Sounds like I may need to change the SQL...
7
15658
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 of the table on one screen - thus eliminating the need to use the horizontal scroll bar to view...
11
76242
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 into: (1) When the data in the list updated, the data on the screen did not update. (2) When I...
6
3219
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
9152
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9930
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9571
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7116
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4996
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.