Solved

DataGridView Button Column Event Handling

Posted on 2006-06-14
4
7,656 Views
Last Modified: 2012-08-13
Hi experts,

I have a datagridview control that displays records from a table.

I have added a Button Column to the datagridview.

What i want to happen is when the button is pressed, it sets the value of the first column to a string, opens a new form, and filters the results using that string.

The main part I am having trouble with is handling the event of a button click and setting the string.

The string has to be the value of the very left column on the SAME row.

Can anyone help?

Bryan
0
Comment
Question by:bryanford
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
Mystify earned 250 total points
ID: 16910479
What you are asking is a little confusing.
Here is my best guess



    ' Assumes that your "button column" is called "Column4"
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        ' look to see if my button column was the column clicked. Space bar will also trigger this event.
        If e.ColumnIndex = Column4.Index Then

            ' Get the index of the leftmost column.
            Dim LeftModeColumnIndex As Integer = DataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Index

            ' What i want to happen is when the button is pressed, it sets the value of the first column to a string
            ' Ok.
            DataGridView1.Rows(e.RowIndex).Cells(LeftModeColumnIndex).Value = ""

            ' Put code here to open the "new form" and do whatever it is in that form you want to do
            Dim frm As New Form1
            Using frm
                frm.ShowDialog()
            End Using

            ' The string has to be the value of the very left column on the SAME row.
            ' Get the contents of the first column
            Dim s As String = ""
            If DataGridView1.Rows(e.RowIndex).Cells(LeftModeColumnIndex).Value IsNot Nothing Then
                s = DataGridView1.Rows(e.RowIndex).Cells(LeftModeColumnIndex).Value
            End If

            ' So now you have the string value of hte very leftmost column, but I don't know what
            ' you want to do with it, If you don't set it to anything after loading the form, then it will still be empty.
        End If
    End Sub
0
 
LVL 1

Author Comment

by:bryanford
ID: 16910565
I am not really sure how to go about it yet, but the second form is a more detailed view of the datagrid and the string will be the primary key from a database. the purpose of setting it to a string is so i can hopefully add a WHERE statement to the sql when loading the second form and using the string as the parameter.

im not really good in wording what i think in my mind sorry :)

Bryan
0
 
LVL 1

Author Comment

by:bryanford
ID: 16910568
anyway ill try that solution when i get to work tomorrow and let you know. but looks right to me. thanks :)
0
 
LVL 1

Author Comment

by:bryanford
ID: 16926374
yep. works great. thanks very much!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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