Rudemusik

Hello All!

I have a datagrid in my windows form, working in vb.net. I made the "First Name" a label link. What I would like to do is the same thing I can do in a web application. Click on the link, and the details of that record fill another small datagrid in the second form. Can sombody show me a sample or point me in a direction on how I can do this in a windows form.

Thanks!

Rudy



Re: Windows Forms Data Controls and Databinding Using a link in a datagrid view.

Yu Guo – MSFT

Hi, Rudemusik,

Based on my understanding, you want to open a new form with details when you click some cell in your DataGrid, don't you

This could be done with DataGrid.HitTestInfo and a Custom Constructor.

1. In Form1, we will have a DataGrid to hold all the Data, and in its MouseDown event, we will find out which cell is clicked.

Code Block

Public Class Form1

Dim datatable As New DataTable("data")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

datatable.Columns.Add("col1")

datatable.Columns.Add("col2")

datatable.Rows.Add("ABC", "bcd")

datatable.Rows.Add("bcd", "ABC")

Me.DataGrid1.DataSource = datatable

End Sub

Private Sub DataGrid1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown

Dim info As DataGrid.HitTestInfo

info = Me.DataGrid1.HitTest(e.X, e.Y) 'Test the currently clicked place

If info.Type = DataGrid.HitTestType.Cell And info.Column = 0 Then 'If the first column cells are clicked

Dim dv As DataView

dv = datatable.Copy().DefaultView

dv.RowFilter = "col1='" + Me.DataGrid1.Item(info.Row, info.Column).ToString() + "'"

Dim detail As New Form2(dv) 'Send the selected row to form2

detail.ShowDialog()

detail.Dispose()

End If

End Sub

End Class

2. in Form2, we have another DataGrid to show the details, and we will have to write its constructor.

Code Block

Public Class Form2

Public Sub New(ByVal Data As DataView)

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

Me.DataGrid1.DataSource = Data

End Sub

End Class

More info,

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagrid.hittestinfo.aspx

Hope this helps,

Regards






Re: Windows Forms Data Controls and Databinding Using a link in a datagrid view.

Rudemusik

Hi Yu!

Thanks for the reply. I have changed things a little bit. I have a datagrid that gets filled with a SP. I want to fill another datagrid whit a sp, but it has a parameter of the ID. So I'm thinking when I call the double click event for that row, it should read the Order_Id col, and pass that to the next form so when that SP is looking for @Order_ID, it can fill that datagrid.

How do I pass that value to the next datagrid

Thanks!

Rudy





Re: Windows Forms Data Controls and Databinding Using a link in a datagrid view.

Yu Guo – MSFT

Hi, Rudemusik,

In that case, you can change the constructor as this

Code Block

Public Sub New(byval data As String)

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

'You codes to get the DataSource, use data as parameter

Me.DataGrid1.DataSource = YourReturnedDataSource

End Sub

And call it like

Code Block

Dim DataGrid1 As New DataGrid

Dim detail As New Form2(Me.DataGrid1.Item(info.Row, info.Column).ToString()) 'Send the selected row to form2

detail.ShowDialog()

detail.Dispose()

Hope this helps,

Regards