DataGridView Get ID

AccessDev

New member
Joined
Sep 12, 2007
Messages
3
Programming Experience
Beginner
Dear All Experienced VB.NET Programmers,

I am new to vb.net and I am using VS 2005 Professional and have a DGV bound to a table.

I can upload my binary data to the DB Table and display the results in the DGV.

I have a textbox called textbox1 and pass the textbox1.text to the sql query to download the related data from the DB.

e.g
Dim da As New SqlDataAdapter("SELECT ImgField FROM MyImages WHERE ID = " & TextBox1.Text, con)

I click the download button and the file downloads.

Is there a way I can dynamically get the ID from the Click event on the DGV.

I hope this makes sense. I can elaborate more if needed.
 
yes, you need to use the e.RowIndex and e.ColumnIndex

something along the lines of (this may not be right, I use an addon grid which makes getting column values very simple)

dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString()


that should be a starting point for you, as I say, the wording may not be correct...
 
using the follwoing code.
TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).FormattedValue.ToString()

the user must click the text in the Cell ID column to get the ID. I need to get the ID of the row no matter what cell was clicked by the user. Thanks for your reply.
 
I can upload my binary data to the DB Table and display the results in the DGV.
Ugh.

I have a textbox called textbox1 and pass the textbox1.text to the sql query to download the related data from the DB.

e.g
Dim da As New SqlDataAdapter("SELECT ImgField FROM MyImages WHERE ID = " & TextBox1.Text, con)
Yeah, dont do that. To find out why not, read the PQ link in my signature. TO find out how to do that properly, read the DW2 link in my signature, section "Creating a SImple Data App" then "Creating a Form to Search Data"


Is there a way I can dynamically get the ID from the Click event on the DGV.
You wouldnt do that; your table is bound to your DGV through a BIndingSOurce (or, it should be), and when the .Position of the BindingSOurce changes, indicating the user is looking at another row, to can detect that. If you want the download button method still then you query the BindingSource for its .Current property to get the current row the user is looking at.
 
Thanks for the links. I can see this is not good coding practise. Your reply is just what I am looking for. I want the user to highlight the row of interest then click the download button for the PDF to download.
 
OK, if you promise not to write bad code ever again :D then you can fiddle your existing code for now:

VB.NET:
TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells([B][SIZE="6"]0[/SIZE][/B]).FormattedValue.ToString()

This is how we always access the ID in Column 0, no matter where the user clicked (kick self first, then learn new better way)


New better code would look like:

VB.NET:
Private PreadOnly Property CurrentPDFRow
 Get 
  If PDFBindingSource.Current Is Nothing Then Return Nothing

  Return DirectCast(DirectCast(PDFBindingSource.Current, DataRowView).Row, PDFRow)
 End Get
End Property

Sub DownloadButton_Click(...)
 If CurrentPDFRow Is Nothing Then 'message the user to click on something!
 Else
   DownloadPDF(CurrentPDFRow.PDFName)
  EndIf
End Sub

Assumptions:
You made a typed dataset from a source table called PDF
It contains a column called PDFName
You dragged it into the form and let the VS2005 set up all the bindings and everything for you.

This approach would take me (a seasoned VB developer and Data Access nerd) about 3 minutes to code.. It really is that easy. Read the DW2 link.
 
Back
Top