dcsimg
Results 1 to 12 of 12
  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    Resolved [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.

  2. #2
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    Re: Working with an unbound DatagridView

    Thank you, that worked. Should of known it was something simple like that!

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,851

    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.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,907

    Re: [RESOLVED] Working with an unbound DatagridView

    Quote Originally Posted by si_the_geek View Post
    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:
    1. outputString = String.Join(",",
    2.                            dgvOutput.Rows(rctr).
    3.                                      Cells.
    4.                                      Cast(Of DataGridViewCell)().
    5.                                      Select(Function(dgvc) dgvc.Value))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    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.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    430

    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)

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,907

    Re: [RESOLVED] Working with an unbound DatagridView

    Quote Originally Posted by dschaldach View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    Re: [RESOLVED] Working with an unbound DatagridView

    Quote Originally Posted by kpmc View Post
    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.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,907

    Re: [RESOLVED] Working with an unbound DatagridView

    Quote Originally Posted by dschaldach View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Re: [RESOLVED] Working with an unbound DatagridView

    Quote Originally Posted by jmcilhinney View Post
    .... 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 ?

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,907

    Re: [RESOLVED] Working with an unbound DatagridView

    Directly:
    vb.net Code:
    1. myDataGridView.DataSource = myDataTable
    Via a BindingSource:
    vb.net Code:
    1. myBindingSource.DataSource = myDataTable
    2. myDataGridView.DataSource = myBindingSource
    A BindingSource brings together various functionality related to bound data from various places into one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.