Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Capture data from datagridview to display into a texbox with a click of a button
 

Capture data from datagridview to display into a texbox with a click of a button

Hi all,

I am doing my project in windows form and am using Microsoft Visual Studio 2005. I have a little problem which i hope any of you guys can help. I need to capture data(Email Column) in my datagridview(in form2.vb) to display into a textbox of another windows form(form1.vb) with a click of a button(Add Contact button) after i checked the checkboxes in the Select column. My datagridview contains the columns CustomerName , Email and Select(Boolean) .

Thank you in advance.

Md Azmil  Friday, July 10, 2009 8:53 AM

I am sorry I had a typo in the code..

For the 1st Error: Use Form1.TxtFrom.Clear()
For the 2nd Error: Form1.TextBox1.Text = ds.Tables(0).Rows(i).Item("Email").ToString & Vbcrlf

I have edited the code in my earlier post.. I again apologize for the typos..


Ashray Lavsi
If my post answers your Question, don't forget to "Mark it as Answer"
Ashray Lavsi  Monday, July 13, 2009 12:10 PM
Well, you *do* have a DataSet and you *do* have a SQL connection, according to the code you posted above. At least one of your problems is in the following code:

Dim i As Integer
Dim ds As New DataSet

'Assuming ds is the DataSet you have used to populate the DataGrid

Form1.txtFrom.Clear()
Form1.txtFrom.Text = ""

For i = 0 To (DataGridView1.Rows.Count - 1)
If ds.Tables(0).Rows(i).Item("SELECT") = True Then
Form1.txtFrom.Text = ds.Tables(0).Rows(i).Item("Email").ToString & vbCrLf
End If
Next

Notice your comment where you say 'Assuming ds is the DataSet you have used to populate the DataGrid but you just created that DataSet, it doesn't contain any Tables, hence the reason for the error (Error Message: IndexOutOfRangeException was unhandled Cannot find table 0. ).

Since you actually want to use the DataSet (more likely DataTable) used to populate the DataGrid, you could use this code instead for ds:

Dim ds As DataSet = CType(DataGridView1.DataSource, DataTable)

But, actually what might be better is touse the DataGridViewRow instead, so your code might look something like this:

For i = 0 To (DataGridView1.Rows.Count - 1)
If DataGridView1.Rows(i).Cells("SELECT").Value = True Then
Form1.txtFrom.Text = DataGridView1.Rows(i).Cells("Email").Value.ToString & vbCrLf
End If
Next


~~Bonnie Berent [C# MVP]
BonnieB  Tuesday, July 14, 2009 4:48 AM

That's because it was probably a DataTable, not a DataSet. I started to post my example using a DataSet, changed my mind midstream and changed it to DataTable. I missed that one line that you corrected, but you corrected it the wrong way. Should have been this:

Dim ds As DataTable = CType(DataGridView1.DataSource, DataTable)

Sorry for the confusion.


~~Bonnie Berent [C# MVP]
  • Marked As Answer byMd Azmil Tuesday, July 21, 2009 2:08 AM
  •  
BonnieB  Saturday, July 18, 2009 4:47 PM

Well, first of all, this is the first time you've posted code that shows that you set the Grid.DataSource to a BindingSource object. I had been assuming all along that it was your DataTable.

Second of all, I think you missed the entire point of my post, which was to give you a couple of options ... eitherdo itone way(iterating throughthe DataTable.Rows collection) or the other way (iterating through the DataGridViewRows collection)and you ended up mixing the two together.

Notice in your code that you're not even using the the DataTable now, you've opted to iterate through the DataGridView.Rows collection. You can totally remove that line of code that's crashing. It's unnecessary.


~~Bonnie Berent [C# MVP]
  • Marked As Answer byMd Azmil Tuesday, July 21, 2009 2:08 AM
  •  
BonnieB  Monday, July 20, 2009 4:13 AM
Is the textbox in form1 multi-line ?!? You want to display the Email of all the Selected Customers in one textbox ??


If my post answers your Question, don't forget to "Mark it as Answer"
Ashray Lavsi  Friday, July 10, 2009 1:28 PM
how do i know its multi-line? From properties?

And yes please, i want to display the Email of all selected customers in one textbox.....maybe each email address is seperated by commas.
Md Azmil  Friday, July 10, 2009 4:24 PM
You can set the TextBox's property to be Multiline, that way you can separate the email addresses with a return.. Other option would be to use a RichTextBox (which is multiline by default) !!

You can add the following code to your button_click event:

Dim i as Integer

'Assuming ds is the DataSet you have used to populate the DataGrid

Form1.TextBox1.Clear()
Form1.TextBox1.Text = ""

For i = 0 to (DGV.Rows.Count - 1)
   If ds.Tables(0).Rows(i).Item("SELECT") = TRUE Then
      Form1.TextBox1.Text = ds.Tables(0).Rows(i).Item("Email").ToString & Vbcrlf   
End If
Next



If my post answers your Question, don't forget to "Mark it as Answer"
  • Edited byAshray Lavsi Monday, July 13, 2009 12:10 PMRemoved Typos
  • Edited byAshray Lavsi Monday, July 13, 2009 12:11 PMRemoved Typos
  •  
Ashray Lavsi  Friday, July 10, 2009 5:24 PM
ok i will test it tomorrow at work and will "Mark it as Answer" if your post answers my questions. Thank you so much for your time.
Md Azmil  Saturday, July 11, 2009 4:59 PM
I have tested the codes but seems to return errors. There are 2 errors:-

Form1.txtFrom.Text.Clear()
Error 1 message: 'Clear' is not a member of 'String'

UserDataSet.Tables(0).Rows(i).Item
Error 2 message: Overload resolution failed because no accessible 'Item' accepts this number of arguments




Below are my codes for the windows form. I have a datagridview(Datagridview1) and a button(btnAddContact) in my form.

Public Class Form2

    Public Event ItemSelected(ByVal Item As String)

    Private Sub btnAddContact_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddContact.Click

        Dim i As Integer

        'Assuming ds is the DataSet you have used to populate the DataGrid

        Form1.txtFrom.Text.Clear()
        Form1.txtFrom.Text = ""

        For i = 0 To (DataGridView1.Rows.Count - 1)
            If UserDataSet.Tables(0).Rows(i).Item("SELECT") = True Then
                Form1.txtFrom.Text = UserDataSet.Tables(0).Rows(i).Item & vbCrLf("Email").ToString
            End If
        Next


    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'UserDataSet.CustDetail' table. You can move, or remove it, as needed.
        Me.CustDetailTableAdapter.Fill(Me.UserDataSet.CustDetail)


    End Sub

End Class



Md Azmil  Monday, July 13, 2009 1:26 AM

I am sorry I had a typo in the code..

For the 1st Error: Use Form1.TxtFrom.Clear()
For the 2nd Error: Form1.TextBox1.Text = ds.Tables(0).Rows(i).Item("Email").ToString & Vbcrlf

I have edited the code in my earlier post.. I again apologize for the typos..


Ashray Lavsi
If my post answers your Question, don't forget to "Mark it as Answer"
Ashray Lavsi  Monday, July 13, 2009 12:10 PM
Thank you for the correction. I've edited the codes and debugged the program. After checking a few checkboxes to insert data from the datagridview to the textbox of another form, I pressed the 'Add Contact' button and there is an error message in this line:-

If ds.Tables(0).Rows(i).Item("SELECT") = True Then
Error Message: IndexOutOfRangeException was unhandled
Cannot find table 0.



Below are my codes for the windows form:-

Public Class Form2

    Public Event ItemSelected(ByVal Item As String)

    Private Sub btnAddContact_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddContact.Click

        Dim i As Integer
        Dim ds As New DataSet

        'Assuming ds is the DataSet you have used to populate the DataGrid

        Form1.txtFrom.Clear()
        Form1.txtFrom.Text = ""

        For i = 0 To (DataGridView1.Rows.Count - 1)
            If ds.Tables(0).Rows(i).Item("SELECT") = True Then
                Form1.txtFrom.Text = ds.Tables(0).Rows(i).Item("Email").ToString & vbCrLf
            End If
        Next


    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'UserDataSet.CustDetail' table. You can move, or remove it, as needed.
        Me.CustDetailTableAdapter.Fill(Me.UserDataSet.CustDetail)


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Form3 As New Form3
        Form3.Show()

    End Sub

End Class
Md Azmil  Tuesday, July 14, 2009 1:39 AM

How are you filling your dataset can you post your code ?


Ashray Lavsi
If my post answers your Question, don't forget to "Mark it as Answer"
Ashray Lavsi  Tuesday, July 14, 2009 2:50 AM
I'm sorry but i do not have any dataset or any SQL connection as i used the 'Choose Data Source' wizard. Do you mind helping me? I am using Microsoft SQL Server 2005 and i need to get the datagridview to read from SQL Server 2005.

Thank you.
Md Azmil  Tuesday, July 14, 2009 3:55 AM
Well, you *do* have a DataSet and you *do* have a SQL connection, according to the code you posted above. At least one of your problems is in the following code:

Dim i As Integer
Dim ds As New DataSet

'Assuming ds is the DataSet you have used to populate the DataGrid

Form1.txtFrom.Clear()
Form1.txtFrom.Text = ""

For i = 0 To (DataGridView1.Rows.Count - 1)
If ds.Tables(0).Rows(i).Item("SELECT") = True Then
Form1.txtFrom.Text = ds.Tables(0).Rows(i).Item("Email").ToString & vbCrLf
End If
Next

Notice your comment where you say 'Assuming ds is the DataSet you have used to populate the DataGrid but you just created that DataSet, it doesn't contain any Tables, hence the reason for the error (Error Message: IndexOutOfRangeException was unhandled Cannot find table 0. ).

Since you actually want to use the DataSet (more likely DataTable) used to populate the DataGrid, you could use this code instead for ds:

Dim ds As DataSet = CType(DataGridView1.DataSource, DataTable)

But, actually what might be better is touse the DataGridViewRow instead, so your code might look something like this:

For i = 0 To (DataGridView1.Rows.Count - 1)
If DataGridView1.Rows(i).Cells("SELECT").Value = True Then
Form1.txtFrom.Text = DataGridView1.Rows(i).Cells("Email").Value.ToString & vbCrLf
End If
Next


~~Bonnie Berent [C# MVP]
BonnieB  Tuesday, July 14, 2009 4:48 AM
For the part:-

Dim ds As DataSet = CType(DataGridView1.DataSource, DataTable)

There is an error to this line which is : Value of type 'System.Data.DataTable' cannot be converted to 'System.Data.DataSet'.


After which i changed it to this and there are no errors:-

Dim ds As DataSet = CType(DataGridView1.DataSource, DataSet)  


But after i debug, checked a few checkboxes in the DGV and clicked the 'Add Contact' button, there is an error highlighting this line:-

Dim ds As DataSet = CType(DataGridView1.DataSource, DataSet)

Error message: InvalidCastException was unhandled
Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataSet'.

Please advise.

Md Azmil  Tuesday, July 14, 2009 6:23 AM

That's because it was probably a DataTable, not a DataSet. I started to post my example using a DataSet, changed my mind midstream and changed it to DataTable. I missed that one line that you corrected, but you corrected it the wrong way. Should have been this:

Dim ds As DataTable = CType(DataGridView1.DataSource, DataTable)

Sorry for the confusion.


~~Bonnie Berent [C# MVP]
  • Marked As Answer byMd Azmil Tuesday, July 21, 2009 2:08 AM
  •  
BonnieB  Saturday, July 18, 2009 4:47 PM
Thank you.

I've edited the line that you corrected. After checking a few checkboxes, I click on the 'Add Contact ' button and there is still an error:-

InvalidCastException was unhandled
Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.


Below are my full codes.

Please advise.

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

    Private da As SqlDataAdapter
    Private conn As SqlConnection
    Private bsource As BindingSource = New BindingSource()
    Private ds As DataSet
    Private sql As String
    Private cnString As String

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connectionString As String = "Data Source=ITCU-21\SQLEXPRESS;Initial Catalog=User;" & "Integrated Security=SSPI;"
        conn = New SqlConnection(connectionString)
        sql = "SELECT * FROM CustDetail"

        da = New SqlDataAdapter(sql, conn)
        conn.Open()
        ds = New DataSet()
        Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
        da.Fill(ds, "CustDetail")
        bsource.DataSource = ds.Tables("CustDetail")
        DataGridView1.DataSource = bsource

    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click


        Dim i As Integer
        Dim ds As DataTable = CType(DataGridView1.DataSource, DataTable)



        For i = 0 To (DataGridView1.Rows.Count - 1)
            If DataGridView1.Rows(i).Cells("Selection").Value = True Then
                Form2.TextBox1.Text = DataGridView1.Rows(i).Cells("Email").Value.ToString & vbCrLf
            End If
        Next
    End Sub
End Class
Md Azmil  Monday, July 20, 2009 2:08 AM

Well, first of all, this is the first time you've posted code that shows that you set the Grid.DataSource to a BindingSource object. I had been assuming all along that it was your DataTable.

Second of all, I think you missed the entire point of my post, which was to give you a couple of options ... eitherdo itone way(iterating throughthe DataTable.Rows collection) or the other way (iterating through the DataGridViewRows collection)and you ended up mixing the two together.

Notice in your code that you're not even using the the DataTable now, you've opted to iterate through the DataGridView.Rows collection. You can totally remove that line of code that's crashing. It's unnecessary.


~~Bonnie Berent [C# MVP]
  • Marked As Answer byMd Azmil Tuesday, July 21, 2009 2:08 AM
  •  
BonnieB  Monday, July 20, 2009 4:13 AM

You can use google to search for other answers

Custom Search

More Threads

• Prevent Updates
• [c#]Printing DataGridView
• Trouble with filtering parent table with DataGridView and typed Dataset
• Check row update for DataGridView in VB.net
• Definitively remove records from Access database using VB.NET
• Set SelectedItem on DataGridViewComboBoxColumn When DataSource is Set
• DGV checkbox question
• Cell value disappears when user clicks a button
• Duplicate records in bindingsource following an edit.
• DataGridView population of non-bound columns