Results 1 to 7 of 7

Thread: [RESOLVED] How to keep column of GridView hidden but still get the data ?

  1. #1

    Thread Starter
    New Member lelouch's Avatar
    Join Date
    Mar 2020
    Posts
    9

    Resolved [RESOLVED] How to keep column of GridView hidden but still get the data ?

    hello, I'm working with vs2017 and access as my database I'm developing a windows application. What I'm trying to do is to hide the last column from datagridview which holds data of photos but whenever the user clicks a row it should be also retrieved. How can I make this possible?

    below is my code to retrieve the data from data source and display it in the datagridview. I want to hide the last column from displaying but still be able to retrieve the data from it.


    Code:
      Try
    
                Dim Nform As New Form2
    
                iPOSITION = dgvContestant.CurrentCell.RowIndex
    
                Dim arrImage() As Byte
    
                Nform.txtLastName2.Text = dgvContestant.Item(1, iPOSITION).Value.ToString
                Nform.txtFirstName2.Text = dgvContestant.Item(2, iPOSITION).Value.ToString
                Nform.txtMiddleName2.Text = dgvContestant.Item(3, iPOSITION).Value.ToString
                Nform.cmbStatus2.Text = dgvContestant.Item(4, iPOSITION).Value.ToString
                Nform.txtAddress2.Text = dgvContestant.Item(5, iPOSITION).Value.ToString
                Nform.txtMobile2.Text = dgvContestant.Item(6, iPOSITION).Value.ToString
                Nform.txtTel2.Text = dgvContestant.Item(7, iPOSITION).Value.ToString
                Nform.cmbGender2.Text = dgvContestant.Item(8, iPOSITION).Value.ToString
                Nform.txtAge2.Text = dgvContestant.Item(9, iPOSITION).Value.ToString
    
                If dgvContestant.Item(10, iPOSITION).Value IsNot DBNull.Value Then
    
                    arrImage = dgvContestant.Item(10, iPOSITION).Value
                    Dim mstream As New System.IO.MemoryStream(arrImage)
                    Nform.picBox2.Image = Image.FromStream(mstream)
                    mstream.Close()
    
    
                Else
    
                    Nform.picBox2.Image = Nothing
    
                End If

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to keep column of GridView hidden but still get the data ?

    That is not the code to display anything in a datagridview. If you want to hide a column, set the column's visible property to false. The code you've posted is for retrieving the data from one row and displaying it in some textboxes. You should look into DataBindings. It's the professional way of achieving what your code snippet attempts to do...

    https://docs.microsoft.com/en-us/dot...tframework-4.8

  3. #3

    Thread Starter
    New Member lelouch's Avatar
    Join Date
    Mar 2020
    Posts
    9

    Re: How to keep column of GridView hidden but still get the data ?

    Quote Originally Posted by .paul. View Post
    That is not the code to display anything in a datagridview. If you want to hide a column, set the column's visible property to false. The code you've posted is for retrieving the data from one row and displaying it in some textboxes. You should look into DataBindings. It's the professional way of achieving what your code snippet attempts to do...

    https://docs.microsoft.com/en-us/dot...tframework-4.8
    Hi thanks. I'll try it. So by what you have said you mean that my method of retrieving data from database and putting it into textboxes is wrong and unprofessional?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to keep column of GridView hidden but still get the data ?

    If you read values from your db into a datatable, you can then bind that datatable to a dgv, and use the textbox (and other single value controls) databindings.add method which will work as i suggested when selecting a row in the dgv...

  5. #5

    Thread Starter
    New Member lelouch's Avatar
    Join Date
    Mar 2020
    Posts
    9

    Re: How to keep column of GridView hidden but still get the data ?

    Quote Originally Posted by .paul. View Post
    If you read values from your db into a datatable, you can then bind that datatable to a dgv, and use the textbox (and other single value controls) databindings.add method which will work as i suggested when selecting a row in the dgv...
    Hi thanks for replying. I just want to clarify things, So binding a datatable into my dgv would simplify the work? Can I solve my problem by doing that?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to keep column of GridView hidden but still get the data ?

    Basically yes. Here's a simplified example...

    Code:
    Dim da As OleDb.OleDbDataAdapter
    Dim dt as DataTable
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection("'etc")
    
        'Table1 must have a PrimaryKey
        da = New OleDb.OleDbDataAdapter("SELECT * FROM Table1", connection)
        dt = New DataTable
        da.Fill(dt)
        Dim cb as New OleDb.OleDbCommandBuilder(da)
    
        'bind dgv
        DataGridView1.DataSource = dt
    
        'bind textbox control
        TextBox1.DataBindings.Add("Text", dt, "yourFieldName")
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'save dt
        da.UpDate(dt)
    End Sub

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: How to keep column of GridView hidden but still get the data ?

    Here you go...

    Name:  F2.jpg
Views: 791
Size:  36.5 KB

    Code:
    Imports System.Data.OleDb
    Public Class Form1
        Private bsCategories As New BindingSource
        ''' <summary>
        ''' There are predefined DataGridView columns for
        ''' CategoryName, Description
        ''' </summary>
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim dt As New DataTable
            Const connectionString =
                      "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NorthWind.accdb"
    
            Using cn As New OleDbConnection With {.ConnectionString = connectionString}
                Using cmd As New OleDbCommand With {.Connection = cn}
    
                    cmd.CommandText =
                        "SELECT CategoryID, CategoryName, Description, Picture FROM Categories;"
    
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                End Using
            End Using
    
            bsCategories.DataSource = dt
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = bsCategories
            PictureBox1.DataBindings.Add(New Binding("Image", bsCategories, "Picture", True))
        End Sub
    End Class

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