-
Mar 29th, 2020, 10:48 PM
#1
Thread Starter
New Member
[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
-
Mar 31st, 2020, 08:02 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 1st, 2020, 11:22 PM
#3
Thread Starter
New Member
Re: How to keep column of GridView hidden but still get the data ?
Originally Posted by .paul.
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?
-
Apr 2nd, 2020, 11:36 AM
#4
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 4th, 2020, 02:26 AM
#5
Thread Starter
New Member
Re: How to keep column of GridView hidden but still get the data ?
Originally Posted by .paul.
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?
-
Apr 4th, 2020, 07:04 AM
#6
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
Last edited by .paul.; Apr 4th, 2020 at 07:10 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 4th, 2020, 06:43 PM
#7
Re: How to keep column of GridView hidden but still get the data ?
Here you go...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|