Solved

Code behind for Button Click in Datagridview Windows App

Posted on 2008-06-15
7
2,770 Views
Last Modified: 2013-12-20
Hey there.

This is for a windows App made in VS2008.
I've added a button column in a datagridview, when clicked I would like to get the ID of the record selected. The ID of the record is the second column along, next to the select button. Been playing around with findcontrol but I'm not getting very far.

Thanks
0
Comment
Question by:garethh86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:scgstuff
ID: 21790448
You can always use the DataKeynames and put the recordID in as the key.  Then you should be able to say

dim myID as string
myID = DataGridView1.Rows(e.rowindex).cells(1).value.tostring

Shawn
0
 
LVL 5

Expert Comment

by:scgstuff
ID: 21790455
BTW...Ignore the first part about DataKeyNames.  I started thinking about GridView with web stuff, not the DataGridView.  Sorry about that if it was confusing....I just went the wrong way.  You can just keep the DataGridView the way it is and use this.

Shawn
0
 
LVL 7

Author Comment

by:garethh86
ID: 21791643
How do I access the button though if it is made at runtime? I need code for finding that control, sorry, my fault, didn't make that clear enough in the question.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
scgstuff earned 500 total points
ID: 21799015
Here you go, try this....

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // find out which column was clicked
            if (dataGridView1.Columns[e.ColumnIndex] == Column1)
            {
                string strMessage = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                MessageBox.Show(strMessage);
            }
        }

Shawn
0
 
LVL 7

Author Comment

by:garethh86
ID: 21804258
Sorry, I'm doing this is VB.net, coverted to code below but 'column1' is coming up as not declared, would you know what it should be changed to for this to work in vb.net? Thanks for your help so far!

    Private Sub dataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        ' find out which column was clicked
        If DataGridView1.Columns(e.ColumnIndex) = Column1 Then
            Dim strMessage As String = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString()
            MessageBox.Show(strMessage)
        End If
    End Sub
0
 
LVL 7

Author Comment

by:garethh86
ID: 21804493
Got it, http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23373255.html

See code below, I'll award points for putting me on the right track, thanks!
  Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim strTest As String
 
        strTest = Me.DataGridView1.CurrentRow.Cells("IDDataGridViewTextBoxColumn").Value
 
        Label6.Text = strTest
 
    End Sub

Open in new window

0
 
LVL 7

Author Comment

by:garethh86
ID: 23735202
Came back to this to refresh my memory, in case anyone else comes across this and is stuck heres the vb.net code for determining which column was clicked:


Private Sub SearchResultsDataGrid_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles SearchResultsDataGrid.CellContentClick
 
        If SearchResultsDataGrid.Columns(e.ColumnIndex).Equals(SearchResultsDataGrid.Columns(0)) Then
            'Insert your code here, assumes that your button control is in the first column.
        End If

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question