Solved

DataGridView Button Column Event Handling

Posted on 2006-06-14
4
7,652 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now