I'm converting a system I wrote from vb6 to vb.net and I'm on day 2
using vb.net so apologies if my approach is ridiculous and I'm
grateful for any advice.
I retrieve data from a stored procedure and load it into a
datagridview using the fill function. Once the datagridview is loaded
I call a function to modify some of the fields. I'm basically
replicating the behaviour of my vb6 application. So I call
ModifyDataGrid. In ModifyDataGrid I look at the value in one column.
If it's >1 I want to put a * beside the number in the column before.
Code below if it helps.
If DG.Columns(OFFICEM2COUNT_COL).ValueType.ToString > CStr
(1) Then
DG_MAItems.CurrentCell = DG_MAItems(OFFICEM2_COL, 3)
DG_MAItems.BeginEdit(1)
'DG_MAItems.CurrentCell.Value = builder.Append
(DG_MAItems.CurrentCell.Value.ToString, "*")
DG_MAItems.EndEdit()
End If
The field that I take back from the db is an integer and when I try to
add a * to an integer the compiler is understandably not having it.
So my questions are:
1. If I try to change the value in the integer column from 4 to 4* am
I changing something in the database? I used a data adapter and
dataset to load information. Code below.
Dim da As SqlDataAdapter = New SqlDataAdapter
da.SelectCommand = cmd ' Execute the command
Dim ds As DataSet = New DataSet
da.Fill(ds, "Results")
2. I'm confused about data types. VB6 let me away with such happy
but sloppy messing with data types. How should I convert the integer
in the column so that I can append a * to it?
Thanks for any help.
LL
It is my impression that filling in a DataGridVeiw with a DataSet does
not bind the data in the grid to the source. You need to setup Dat
Binding to do that.
>
> 2. I'm confused about data types. VB6 let me away with such happy
> but sloppy messing with data types. How should I convert the integer
> in the column so that I can append a * to it?
A DataGridViewCell's Value property is an Object. So it really doesn't
matter if the current value is numeric or alphabetic or alphanumeric.
To append a character to the value you will need to apply the .ToString
() method to the cell and add the character to it;
dataGridViewRows[rowindex].Cells[cellindex].Value = dataGridVew.Rows
[rowindex].Cells[cellindex].Value.ToString() + "*"
(untested!!)
Thanks for your response. Unfortunately that didn't work. I know I'm
doing something silly wrong.
I used the code you suggested and I can assign it to a string variable
as below so you're right that there's no problem appending a string
the tostring value of the object.
Dim temp As String
temp = DataGridView.Rows(2).Cells(OFFICEM2_COL).Value.ToString
+ "*"
What I can't do is assign that string to the cell in the data grid
view. I think this is where I'm missing something basic. So the line
of code below gives the error below
DG_MAItems.Rows(2).Cells(OFFICEM2_COL).Value = temp
Error: System.exception: 155* is not a valid value for Int32. -->
System.FormatException: Input string was not in a correct format....
The problem I'm having isn't being caused by the grid not being
editable because if I try to append to a field that's a string in the
database there's no problem.
Thanks again for your help
LL
I'm not 100% sure, but I think the problem may be because the column in the
query result was numeric. Part of the data binding may include the data
type, and the grid is trying to protect you from entering a value in the
column that can't be written back to the database (even if you don't
actually intend to do so).
If you can change the stored procedure, you might try casting that column in
the query to a string type, so that the grid thinks that it's a string
value. If you can't change the query, you should be able to add a column to
the grid that isn't bound to a data column. Make the column that has the
real number in it invisible, and put your modified string value into the
unbound column.
--
Jack
Sorry, I am used to C#.NET. I believe the string concatenation
operator for Visual Basic is &. You might also try the String.Concat
method.
Captain Jack you were right. It's a problem related to the data field
type. I can't change the stored procedure so I've done as you
suggested and added a new column and combined the two original columns
in it and then hidden the two original columns. A bit of a cludge but
it'll do me for now while I'm figuring .net out.
Thanks for your help
LL