-
January 30th, 2011, 08:39 AM
#1
vb2010 datagridview auto text
I would like for my datagridview to automatically fill in the column's next row with the previous entry + 1 letter. So say my datagrid is like this:
ID NextField
15-3A xxxxx
15-3B xxxxx
15-3C xxxxx
Everything is working fine if I type it in but would be nice if the user didn't have to type this each time and just have it fill in the next consecutive ID automatically when you press down to start a new row. Also i need it to work no matter what gets typed in... so if I make another ID 15-4A it will start filling in 15-4B etc. on each new row.
Any Ideas?
Thanks
-
January 30th, 2011, 03:02 PM
#2
Re: vb2010 datagridview auto text
Well, you should be using a GUID to identify each key. Then, you can assign/change each generated number when you want, even if editing is not allowed!
Link tables via their GUID, and you'll have no problems with keys
-
February 8th, 2011, 06:32 PM
#3
Re: vb2010 datagridview auto text
I have looked at it and I'm just not able to understand how to do it, I've never worked with GUID's. I'm using MS Access as my database, my key is just a text type, GUID is not an option. Is there some code example you can think of that will make my datagridview "ID" column's next row default the previous row's ID + the next consecutive letter?
-
February 8th, 2011, 08:37 PM
#4
Re: vb2010 datagridview auto text
Only if you code it by hand..
-
February 9th, 2011, 02:37 PM
#5
Re: vb2010 datagridview auto text
That's what I've been needing help with, the code...
I can do the current code which works fine for putting in a default value:
Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) _
Handles DataGridView1.DefaultValuesNeeded
With e.Row
.Cells("ID").Value = "15-4A"
End With
End Sub
Can anyone think of a way to modify it to work with my application? I need the value of "15-4A" to be the "last row + next consecutive letter" so the datagrid will fill in "15-4B", "15-4C", etc., not keep repeating "15-4A"?
Thanks
-
February 9th, 2011, 04:17 PM
#6
Re: vb2010 datagridview auto text
Sure. Each letter has a numerical value, so you'd use the lowest A or a, to the upper z or Z.
You'd have to write a module to generate the value by hand. Pass the current value, and add one. You have to know what to do when it overflows, though
-
February 10th, 2011, 02:09 PM
#7
Re: vb2010 datagridview auto text
I'm not concerned with overflow... I won't have more than 10 letters used. One last thing I thought of, is there a default expression for access that may be easier than coding all this in? If so, what would it be?
Thanks
-
February 11th, 2011, 12:47 PM
#8
Solved vb2010 datagridview auto text
I don't know if this is the "best" way to do it, but it got the job done for anyone looking to do this. Maybe someone can come up with a much more simple way, I'm a noob! I had to hide a textbox in the form too, to make this work.
Private Sub ConBreakDataDataGridView_DefaultValuesNeeded(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) _
Handles ConBreakDataDataGridView.DefaultValuesNeeded
'Make your textbox display the datagridview cell
TextBox1.Text = Me.DataGridView.CurrentRow.Cells("ID").Value.ToString
Dim cnt As Integer
' count the characters in textbox1
cnt = Len(TextBox1.Text)
'Selects from the left always one character shorter than the last character in your field.
'Do this for however many characters you want to have in your field (I have 12).
Dim string1 As String = Microsoft.VisualBasic.Left(Me.TextBox1.Text, 0)
Dim string2 As String = Microsoft.VisualBasic.Left(Me.TextBox1.Text, 1)
'Select only the last character in your field (from the right).
Dim chglast As String = Microsoft.VisualBasic.Right(Me.TextBox1.Text, 1)
'Make your textbox display your left string and your right string
If cnt = 1 Then
Me.TextBox1.Text = string1 & (Chr(Asc(chglast) + 1))
ElseIf cnt = 2 Then
Me.TextBox1.Text = string2 & (Chr(Asc(chglast) + 1))
'Add one of these statements for every string you have (I have 12)
'Limit the size of your character field
ElseIf cnt > 12 Then
MsgBox("Please use 12 or less characters.")
ElseIf cnt < 1 Then
MsgBox("ID cannot be blank.")
End If
'Make your next row's field display your updated textbox value as default
With e.Row
.Cells("ID").Value = TextBox1.Text
End With
End Sub
-
February 11th, 2011, 02:51 PM
#9
Re: vb2010 datagridview auto text
http://www.freevbcode.com/ShowCode.asp?ID=5440 ???
Code:
Function IncrementString(ByVal strString As String) As String
'
' Increments a string counter
' e.g. "a" -> "b"
' "az" -> "ba"
' "zzz" -> "aaaa"
'
' strString is the string to increment, assumed to be lower-case alphabetic
' Return value is the incremented string
'
Dim lngLenString As Long
Dim strChar As String
Dim lngI As Long
lngLenString = Len(strString)
' Start at far right
For lngI = lngLenString To 0 Step -1
' If we reach the far left then add an A and exit
If lngI = 0 Then
strString = "a" & strString
Exit For
End If
' Consider next character
strChar = Mid(strString, lngI, 1)
If strChar = "z" Then
' If we find Z then increment this to A
' and increment the character after this (in next loop iteration)
strString = Left$(strString, lngI - 1) & "a" & Mid(strString, lngI + 1, lngLenString)
Else
' Increment this non-Z and exit
strString = Left$(strString, lngI - 1) & Chr(Asc(strChar) + 1) & Mid(strString, lngI + 1, lngLenString)
Exit For
End If
Next lngI
IncrementString = strString
Exit Function
End Function
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|