PDA

View Full Version : relation



reza_Nazem
دوشنبه 17 اردیبهشت 1386, 15:08 عصر
سلام دوستان من دو تا جدول را میخواهم در 2 datagridview در یک فرم نمایش دهم . ابتدا باید بین ان دو ralation براقرار کنم کد من کار نمی کنه خواهش می کنم کمکم کنید




Private Sub Create_Ralation()
Dim Col_FactorID As DataColumn
Dim Col_FactorRef As DataColumn

Col_FactorID = DS_Factor.Tables("Factor").Columns("Factor_ID")
Col_FactorRef = DS_Factor.Tables("Optic").Columns("Optic_FactorRef")

Dim Relation As New DataRelation("Relation", Col_FactorID, Col_FactorRef)
DS_Factor.Relations.Add(Relation)
End Sub

reza_Nazem
سه شنبه 18 اردیبهشت 1386, 14:04 عصر
بابا اخه این تشکر کردن داره !!!!! کمک کنید رفقا کارم گیره

ghafoori
سه شنبه 18 اردیبهشت 1386, 21:31 عصر
دوست عزیز این کد اشکالی ندارد منظور دقیق شما از این که کار نمی کنه چیه شما دقیقا چه منظوری دارید که برنامه ان کار را انجام نمی دهد

reza_Nazem
چهارشنبه 19 اردیبهشت 1386, 11:33 صبح
یعنی اینکه اطلاعات را در 2 datagridview نمایش میدهم انگار نه انگار با هم ارتباط دارند . در واقع من انتظار دارم اگه روی یکی ار سطر های grid پدر click کنم و یا در کل حرکت کنم grid فرزند رکوردهای فرزند را فقط نشان دهد . که در برنامه من اصلا این طوری نیست و کل اطلاعات در grid فرزند نمایش داده میشود

ghafoori
چهارشنبه 19 اردیبهشت 1386, 14:36 عصر
در datagridview برای اینکار شما بعد از اینکه دیتاست را پر کردید و با کد پست اول بین انها رابطه ایجاد کردی دو bindingsource درست می کنی هرکدام به یک جدول نسبت می دهی کار تمام می شود مثال msdn را هم برای شما می گذارم که دیگر محکم کاری بشه


Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Form1

Inherits System.Windows.Forms.Form

Private masterDataGridView As New DataGridView()
Private masterBindingSource As New BindingSource()
Private detailsDataGridView As New DataGridView()
Private detailsBindingSource As New BindingSource()

<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub

' Initializes the form.
Public Sub New()

masterDataGridView.Dock = DockStyle.Fill
detailsDataGridView.Dock = DockStyle.Fill

Dim splitContainer1 As New SplitContainer()
splitContainer1.Dock = DockStyle.Fill
splitContainer1.Orientation = Orientation.Horizontal
splitContainer1.Panel1.Controls.Add(masterDataGrid View)
splitContainer1.Panel2.Controls.Add(detailsDataGri dView)

Me.Controls.Add(splitContainer1)
Me.Text = "DataGridView master/detail demo"

End Sub

Private Sub GetData()

Try
' Specify a connection string. Replace the given value with a
' valid connection string for a Northwind SQL Server sample
' database accessible to your system.
Dim connectionString As String = _
"Integrated Security=SSPI;Persist Security Info=False;" & _
"Initial Catalog=Northwind;Data Source=localhost"
Dim connection As New SqlConnection(connectionString)

' Create a DataSet.
Dim data As New DataSet()
data.Locale = System.Globalization.CultureInfo.InvariantCulture

' Add data from the Customers table to the DataSet.
Dim masterDataAdapter As _
New SqlDataAdapter("select * from Customers", connection)
masterDataAdapter.Fill(data, "Customers")

' Add data from the Orders table to the DataSet.
Dim detailsDataAdapter As _
New SqlDataAdapter("select * from Orders", connection)
detailsDataAdapter.Fill(data, "Orders")

' Establish a relationship between the two tables.
Dim relation As New DataRelation("CustomersOrders", _
data.Tables("Customers").Columns("CustomerID"), _
data.Tables("Orders").Columns("CustomerID"))
data.Relations.Add(relation)

' Bind the master data connector to the Customers table.
masterBindingSource.DataSource = data
masterBindingSource.DataMember = "Customers"

' Bind the details data connector to the master data connector,
' using the DataRelation name to filter the information in the
' details table based on the current row in the master table.
detailsBindingSource.DataSource = masterBindingSource
detailsBindingSource.DataMember = "CustomersOrders"
Catch ex As SqlException
MessageBox.Show("To run this example, replace the value of the " & _
"connectionString variable with a connection string that is " & _
"valid for your system.")
End Try

End Sub

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

' Bind the DataGridView controls to the BindingSource
' components and load the data from the database.
masterDataGridView.DataSource = masterBindingSource
detailsDataGridView.DataSource = detailsBindingSource
GetData()

' Resize the master DataGridView columns to fit the newly loaded data.
masterDataGridView.AutoResizeColumns()

' Configure the details DataGridView so that its columns automatically
' adjust their widths when the data changes.
detailsDataGridView.AutoSizeColumnsMode = _
DataGridViewAutoSizeColumnsMode.AllCells

End Sub


End Class