-
Feb 17th, 2018, 12:54 PM
#1
Thread Starter
New Member
[RESOLVED] Working with an unbound DatagridView
I'm rather new to using datagridviews so most likely what i'm doing is wrong but that's why I came here.
I have a program that has some user input and calculations done with the user data and that populates my datagridview no problem. After the user is done entering data I want them to click a button and the program writes all the data to a text file with commas between each cell(example: cell1,cell2,cell3...) so that text file can later be read into another datagridview for reporting the entered data.
The way I tried was to set up and output string similar to:
outputString = dgvOutput.Rows(rctr).Cells(0) &","& dgvOutput.Rows(rctr).Cells(1) &","& dgvOutput.Rows(rctr).Cells(2)
with rctr working as the row index. But when I try to use that code, I get an error telling me that:
"Operator '&' is not defined for types 'System.Windows.Forms.dataGridViewCell' and 'String'.(BC30452)"
I was trying to do it this way so I would have to set up and use ten variables(Yes the datagrid in my program has ten cells).
Any help with this is greatly appreciated.
-
Feb 17th, 2018, 01:38 PM
#2
Addicted Member
Re: Working with an unbound DatagridView
OutputString = dgvOutput.Rows(rctr).Cells(0).ToString & "," & dgvOutput.Rows(rctr).Cells(1).ToString & "," & dgvOutput.Rows(rctr).Cells(2).ToString
-
Feb 17th, 2018, 01:51 PM
#3
Thread Starter
New Member
Re: Working with an unbound DatagridView
Thank you, that worked. Should of known it was something simple like that!
-
Feb 17th, 2018, 02:41 PM
#4
Re: [RESOLVED] Working with an unbound DatagridView
A clearer way to write it would be like this:
Code:
OutputString = dgvOutput.Rows(rctr).Cells(0).Value.ToString & "," & ...
A DataGridViewCell has a lot of different properties (including things like BackColor and more), so specifying which one you actually want (in this case .Value) is a good idea.
As the .Value property can contain different types of data (such as Integers or Dates), it is also a good idea to specify .ToString when you want to use it as a String.
-
Feb 17th, 2018, 10:24 PM
#5
Re: [RESOLVED] Working with an unbound DatagridView
Originally Posted by si_the_geek
A clearer way to write it would be like this:
Code:
OutputString = dgvOutput.Rows(rctr).Cells(0).Value.ToString & "," & ...
A DataGridViewCell has a lot of different properties (including things like BackColor and more), so specifying which one you actually want (in this case .Value) is a good idea.
As the .Value property can contain different types of data (such as Integers or Dates), it is also a good idea to specify .ToString when you want to use it as a String.
One thing to consider there, particularly given that this is an unbound grid, is that that will throw a NullReferenceException if the cell is empty. Personally, I never use more than two concatenation operators in a single expression so I'd be using String.Format or String.Join or String.Concat anyway in this case, but that will also prevent such exceptions in this case. Given that this case calls for a list of values with commas between them, it seems a perfect candidate for String.Join, e.g.
vb.net Code:
outputString = String.Join(",",
dgvOutput.Rows(rctr).
Cells.
Cast(Of DataGridViewCell)().
Select(Function(dgvc) dgvc.Value))
-
Feb 19th, 2018, 12:43 PM
#6
Thread Starter
New Member
Re: [RESOLVED] Working with an unbound DatagridView
[QUOTE=jmcilhinney;5264161]One thing to consider there, particularly given that this is an unbound grid, is that that will throw a NullReferenceException if the cell is empty. Personally, I never use more than two concatenation operators in a single expression so I'd be using String.Format or String.Join or String.Concat anyway in this case, but that will also prevent such exceptions in this case. Given that this case calls for a list of values with commas between them, it seems a perfect candidate for String.Join, e.g.
QUOTE]
I know I marked the issue as resolved since I did get the data to my liking, I looked at your code and had decided to give it a try but I cannot seem to get it working as you have it in your post. Aside from missing a couple closing )'s, I get the error of
'Cast' is not a member of 'System.windows.forms.datagridviewcellcollection'.
As I said the issue has been resolved and there really is no need for a reply to this, as much as I would like you too seeing's how you seem to have a wealth of knowledge on such things.
-
Feb 19th, 2018, 03:16 PM
#7
Re: [RESOLVED] Working with an unbound DatagridView
Is there a reason you do not want to bind the dgv to a datatable?
Another way to get your code cleaned up maybe something like this
Code:
Dim outputString As String = String.Empty
Dim DGV As New DataGridView
For Each DGVRow As DataGridViewRow In DGV.Rows
For Each DGVCol As DataGridViewColumn In DGV.Columns
outputString &= DGVRow.Cells(DGVCol.Name).Value.ToString & ","
Next
Next
outputString = outputString.Substring(0, outputString.Length - 1)
-
Feb 19th, 2018, 07:54 PM
#8
Re: [RESOLVED] Working with an unbound DatagridView
Originally Posted by dschaldach
I know I marked the issue as resolved since I did get the data to my liking, I looked at your code and had decided to give it a try but I cannot seem to get it working as you have it in your post. Aside from missing a couple closing )'s, I get the error of
'Cast' is not a member of 'System.windows.forms.datagridviewcellcollection'.
As I said the issue has been resolved and there really is no need for a reply to this, as much as I would like you too seeing's how you seem to have a wealth of knowledge on such things.
Cast is a LINQ extension method, so you need to have the appropriate assembly reference and namespace import. Most recent projects should already have them by default. Here's the documentation for that Cast method:
https://msdn.microsoft.com/en-us/lib...or=-2147217396
As it says at the top, it's declared in the System.Core.dll assembly and its type is a member of the System.Linq namespace, so you need to reference and import those respectively.
-
Feb 20th, 2018, 11:16 AM
#9
Thread Starter
New Member
Re: [RESOLVED] Working with an unbound DatagridView
Originally Posted by kpmc
Is there a reason you do not want to bind the dgv to a datatable?
Mostly because I've never done so. I've been out of the coding game for some time now and I am fairly rusty and behind on how to do somethings.
-
Feb 20th, 2018, 04:40 PM
#10
Re: [RESOLVED] Working with an unbound DatagridView
Originally Posted by dschaldach
Mostly because I've never done so. I've been out of the coding game for some time now and I am fairly rusty and behind on how to do somethings.
You make life harder for yourself by not binding. I would always recommend data-binding as a first option and only choose not to if there's a specific reason, i.e. data-binding cannot do - or do easily enough - something you need. For most people, it's a case of calling Fill on a data adapter to populate a DataTable, bind that to a DataGridView (either directly or via a BindingSource) by assigning it to the grid's DataSource property, having the user edit via the UI, then calling Update on the same data adapter. There's often no code required to touch the data directly.
-
Feb 21st, 2018, 05:56 AM
#11
Addicted Member
Re: [RESOLVED] Working with an unbound DatagridView
Originally Posted by jmcilhinney
.... For most people, it's a case of calling Fill on a data adapter to populate a DataTable, bind that to a DataGridView (either directly or via a BindingSource) ....
Hi jmc, can you explain directly ?
-
Feb 21st, 2018, 06:08 AM
#12
Re: [RESOLVED] Working with an unbound DatagridView
Directly:
vb.net Code:
myDataGridView.DataSource = myDataTable
Via a BindingSource:
vb.net Code:
myBindingSource.DataSource = myDataTable myDataGridView.DataSource = myBindingSource
A BindingSource brings together various functionality related to bound data from various places into one.
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
|