Solved

DataGridView Button Column Event Handling

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

17 Experts available now in Live!

Get 1:1 Help Now