Results 1 to 6 of 6

Thread: [RESOLVED] Retrieve the text value of a cell in a datagrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    4

    Resolved [RESOLVED] Retrieve the text value of a cell in a datagrid

    What is the easiest way to retrieve the text value of a cell in a datagrid? I have search for a while and don't see anything out there. Thanks for any help.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Retrieve the text value of a cell in a datagrid

    Something like this:
    DirectCast(Datagrid1.DataSource,Dataset).tables(0).Rows(0).Item(0).tostring

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    4

    Re: Retrieve the text value of a cell in a datagrid

    strName = DirectCast(dgResume.DataSource,DataSet).Tables(0).Rows(intRow).Item(0).ToString()

    here is what that line looks like. strName is definded as a string variable. When I run it, I get the System.InvalidCastException: Specified cast is not valid.

    Does anyone know why I am getting this error? Thanks for any help

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    4

    Re: Retrieve the text value of a cell in a datagrid

    Ok I figured it out. Rather then DirectCast from datagrid to dataset, I just directly refer to the dataset the populated the datagrid.

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [RESOLVED] Retrieve the text value of a cell in a datagrid

    I usually just loop through the values of the dataset..
    VB Code:
    1. Dim MyDataSet As New System.Data.DataSet 'you can just replace this with your current dataset
    2.     MyDataSet = DataGrid1.DataSource 'replace with current dataset
    3.             For Each MyRow As System.Data.DataRow In MyDataSet.Tables(0).Rows
    4.                 Dim Mystring As String
    5.                 For Each MyColumn As System.Data.DataColumn In MyDataSet.Tables(0).Columns
    6.                         Mystring = MyDataSet.Tables(0).Rows(I).Item(MyColumn)                    
    7.                 Next
    8.                 msgbox(Mystring)
    9.                 I = I + 1
    10.             Next
    This iterates through all rows and columns of the dataset..... MyDataSet would be a dataset declared = to the datasource of your datagrid

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Retrieve the text value of a cell in a datagrid

    Have you bound the DataGrid to a DataSet or a DataTable? Also note that the data in the grid does not always correspond to the data in the underlying table, because the displayed data actually corresponds to a DataView, which can be filtered and/or sorted. If you've bound the grid to a DataSet then I suggest this code:
    VB Code:
    1. Dim myString As String = DirectCast(myDataGrid.DataSource, DataSet).Tables(myDataGrid.DisplayMember).DefaultView(rowIndex)(columnNameOrIndex).ToString()
    If it is in fact bound to a DataTable then you would use something like this:
    VB Code:
    1. Dim myString As String = DirectCast(myDataGrid.DataSource, DataTable).DefaultView(rowIndex)(columnNameOrIndex).ToString()
    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
  •  



Click Here to Expand Forum to Full Width