PDA

View Full Version : حالت master / detail



amir_king2_2
چهارشنبه 06 اسفند 1382, 07:42 صبح
سلام.
یه حالت master / detail در table ها رو با #C چطور میشه درست کرد ؟

با تشکر.

Mahdavi
چهارشنبه 06 اسفند 1382, 18:20 عصر
دوست عزیز
لطفا بیشتر توضیح دهید.
ساختار اون تو database یا تو Datagrid.
منظورم اینه که ارتباط جداول رو میخوای سطح برنامت باشه یا database ؟
ممنون ؟

amir_king2_2
پنج شنبه 07 اسفند 1382, 07:20 صبح
نه توی DATABASE که میتونم از طریق RELATIONSHEEP ارتباطهای MASTER/DETAIL بوجود بیارم. منظورم توی datagrid بود. مثلا تو دلفی در table ها یه خاصیت mastersource و masterfields هست که میشه حالت master/detail بین جدولها در grid بوجود آورد. میخواستم ببینم آیا #C هم چنین چیزی یا شبیه اون رو داره ؟

Mahdavi
شنبه 09 اسفند 1382, 17:34 عصر
دوست عزیز
تو موقعه Desing Time کافی هردو جدول Master و Detail رو روی یه DataSet بریزی . اونوقت برید رو DataSet کلیک سمت راست کنی و گزینه View Schema... رو انتخاب کنی.
حال تو اون حالت بین دو جدولت یه Relation اضافه کنی.
برای اینکار فیلد کلید Master رو گرفته و به جدول Detail بکشید (البته منظور همون فیلد رابط این دو جدول)
حال اگه تو DataGrid و تو خاصیت DataSource فقط اسم DataSet به تنهایی انتخاب کنی جدول Master دیده میشه و جدول Detil با یه + مشخص شده.
درمورد RunTime اگه خاصتی بگو کدهاشو بنویسم
موفق باشید

danial82
شنبه 14 اردیبهشت 1387, 15:07 عصر
من 3تا جدول تو ديتاست دارم كه جدول دوم يك جدول واسطه تا رابطه n-n رو بشكونه حالا چطوري ميشه جدول اول و سوم رو بصورت master / detail توي دوتا گريد نمايش داد؟

bita_naz
شنبه 14 اردیبهشت 1387, 19:12 عصر
دوست عزیز
تو موقعه Desing Time کافی هردو جدول Master و Detail رو روی یه DataSet بریزی . اونوقت برید رو DataSet کلیک سمت راست کنی و گزینه View Schema... رو انتخاب کنی.
حال تو اون حالت بین دو جدولت یه Relation اضافه کنی.
برای اینکار فیلد کلید Master رو گرفته و به جدول Detail بکشید (البته منظور همون فیلد رابط این دو جدول)
حال اگه تو DataGrid و تو خاصیت DataSource فقط اسم DataSet به تنهایی انتخاب کنی جدول Master دیده میشه و جدول Detil با یه + مشخص شده.
درمورد RunTime اگه خاصتی بگو کدهاشو بنویسم
موفق باشید

بله لطفا کد هاشو بذارید

مرسی

sinpin
یک شنبه 15 اردیبهشت 1387, 06:20 صبح
بله لطفا کد هاشو بذارید
به تاريخ پستها هم توجه كنيد : " شنبه 09 اسفند 1382, 05:04 بعد از ظهر"

danial82
یک شنبه 15 اردیبهشت 1387, 07:37 صبح
من 3تا جدول تو ديتاست دارم كه جدول دوم يك جدول واسطه تا رابطه n-n رو بشكونه حالا چطوري ميشه جدول اول و سوم رو بصورت master / detail توي دوتا گريد نمايش داد؟

یعنی راهی نداره؟

majid325
شنبه 02 آذر 1387, 02:41 صبح
كد زير ارتباط 4 جدول در يه ديتاست رو برقرار ميكنه:

dsService = new DataSet();

//.................................................. .....................................

SqlDataAdapter daService = new SqlDataAdapter("SELECT [SERVICE_Code] FROM [Tb_SERVICE] ", cnn);
daService.Fill(dsService, "Tb_SERVICE");

SqlDataAdapter daServiceList = new SqlDataAdapter("SELECT [ServiceList_Code],[ServiceList_ServiceCode] FROM [Tb_ServiceList]", cnn);
daServiceList.Fill(dsService, "Tb_ServiceList");

SqlDataAdapter daInsurance = new SqlDataAdapter("SELECT [INSURANCE_ID],[INSURANCE_Code],[INSURANCE_LIST_Code] FROM [Tb_INSURANCE]", cnn);
daInsurance.Fill(dsService, "Tb_INSURANCE");

SqlDataAdapter daInsList = new SqlDataAdapter("SELECT [InsList_Code],[InsList_INSURANCE_Code],[InsList_Name] FROM [Tb_InsList]", cnn);
daInsList.Fill(dsService, "Tb_InsList");

//.................................................. .....................................

DataRelation drlServiceAndList = new DataRelation("ServiceAndList", dsService.Tables["Tb_SERVICE"].Columns["SERVICE_Code"], dsService.Tables["Tb_ServiceList"].Columns["ServiceList_ServiceCode"]);
dsService.Relations.Add(drlServiceAndList);

DataRelation drlListAndInsurance = new DataRelation("drlListAndInsurance", dsService.Tables["Tb_ServiceList"].Columns["ServiceList_Code"], dsService.Tables["Tb_INSURANCE"].Columns["INSURANCE_LIST_Code"]);
dsService.Relations.Add(drlListAndInsurance);

DataRelation drlInsuranceAndInsList = new DataRelation("drlInsuranceAndInsList", dsService.Tables["Tb_INSURANCE"].Columns["INSURANCE_Code"], dsService.Tables["Tb_InsList"].Columns["InsList_INSURANCE_Code"]);
dsService.Relations.Add(drlInsuranceAndInsList);

//.................................................. .....................................

BindingSource bsService = new BindingSource(dsService, "Tb_SERVICE");
BindingSource bsServiceList = new BindingSource(bsService, "ServiceAndList");
BindingSource bsInsurance = new BindingSource(bsServiceList, "drlListAndInsurance");
BindingSource bsInsList = new BindingSource(bsInsurance, "drlInsuranceAndInsList");

//.................................................. .....................................


dataGridViewXSERVICE.DataSource = bsService;

dataGridViewXSERVICEList.DataSource = bsServiceList;

dataGridViewXInsurance.DataSource = bsInsurance;

dataGridViewXInsList.DataSource = bsInsList;

ببخشيد چون طولاني بود من فقط يه كم جمع و جورش كردم كه بشه خوندش ، نامها هم وقت نكردم تغيير بدم ، زياد پيچيده نيست.(اگه كسي هم بهينش كرد كه چه بهتر)

Developer Programmer
سه شنبه 06 اسفند 1387, 09:47 صبح
داشتم مثالی رو که مایکروسافت واسه Master/Detail کردن نوشته، میخوندم.
مایکروسافت هم از BindingSource استفاده کرده ... وقتی از BindingSource استفاده میکنی همه چیز درست کار میکنه ؛ اما وقتی دیتاست رو به عنوان DataSource پاس میدی، دیگه کار نمیکنه

واقعا واسه ایجاد Master/Detail یاید از BindingSource استفاده کرد؟



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"
Me.StartPosition = FormStartPosition.CenterScreen

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

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"))

'************************************************* *********
' Important !
'************************************************* ********
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

End Class