Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Master/Detail forms
 

Master/Detail forms

Hello,

How can i create a master/detail form where the detail form won't be a DataGridView but two separated forms connected through a button?

I want to have a button in master form and when the user clicks on it detail form will open and the user will get only the records related to master form.


Any suggestions?

Thank you.
dealwi8me  Saturday, January 10, 2009 7:53 PM

Hi dealwi8me,

I am not quite clear what you are looking for, here is just a very general sample.
Master Form code:

publicpartialclassFrmMaster:Form
{
publicFrmMaster()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
//Transmitsomerelateddatatothedetailformthroughitsconstructor
FrmDetailfrmDetail=newFrmDetail("Relateddata");
frmDetail.ShowDialog();
}
}


Detail Form code:

publicpartialclassFrmDetail:Form
{
publicFrmDetail()
{
InitializeComponent();
}
publicFrmDetail(stringdataFromMaster)
{
InitializeComponent();
//Initializethisformbaseonthedatafrommasterform
}
}


When the detail form is shown, it will show the data only relate to the data send from master form.

If you have any detail question, please tell me. And I will change the code into a more detail one.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, January 12, 2009 9:35 AM
First of all thank you for your answer:)

I will try to be more specific.
I have two tables Customer and Item and a relationship from Customer to Items (1-M). The two tables are connected through CustomerID.

Table: Customer
CustomerID Name LastName
1 John Smith
2 Andy Collins

Table: Items
ItemID CustomerID ItemCode
1 1 CS5433
2 1 CS6571
3 1 CS7891
4 2 KL3355

I created a DataSet (CustomerDS) with Customer, Items and their relationship.
I also created two forms frmCustomer and frmItems based on CustomerDS.
How can i connect the above two forms in order to get only the Items of the specific customer each time?


dealwi8me  Monday, January 12, 2009 3:58 PM

Hi dealwi8me,

Since you didn't tell me which control you want to use, so I cannot give you the exact code sample. Here is the thinking. When you click a customer on frmCustomer, transmit its ID to the frmItems through its constructor.

FrmCustomer code:

publicpartialclassFrmCustomer:Form
{
publicFrmCustomer()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
//Transmitsomerelateddatatothedetailformthroughitsconstructor
//ForexampleyouhaveselectthecustomerwhoseIDis1
FrmItemsfrmItems=newFrmItems(1);
frmItems.ShowDialog();
}
}

FrmItems code:

publicpartialclassFrmItems:Form
{
privateDataSetdataSet=newDataSet();
privateDataTabledataTable=newDataTable();
publicFrmItems()
{
InitializeComponent();
}
publicFrmItems(intcusID,DataSetds)
{
InitializeComponent();
this.dataSet=ds;
this.dataTable=this.dataSet.Tables["Items"];
DataRow[]dr=this.dataTable.Select("CustomerID="+ID.ToString());
//Thus,youhavegotalltherowswhoseCustomerIDequalstotheIDsentfromFrmCustomer
}
}

I used Select method of DataTable to fetch the data from "Items" table whose CustomerID matchs the FrmCustomer selected customer.

DataTable.Select method: http://msdn.microsoft.com/en-us/library/det4aw50.aspx

If you have any question, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, January 13, 2009 2:06 AM

I made the following changes toyour code but i'm still getting all detail data, not only the related.

FrmCustomer code:

publicpartialclassFrmCustomer:Form
{
publicFrmCustomer()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
//Transmitsomerelateddatatothedetailformthroughitsconstructor
//ForexampleyouhaveselectthecustomerwhoseIDis1
FrmItemsfrmItems=newFrmItems(1,customerDS);
frmItems.ShowDialog();
}
}

FrmItems code:

publicpartialclassFrmItems:Form
{
privateDataSetdataSet=newDataSet();
privateDataTabledataTable=newDataTable();
publicFrmItems()
{
InitializeComponent();
}
publicFrmItems(intcusID,DataSetds)
{
InitializeComponent();
this.dataSet=ds;
thisthis.dataTable=this.dataSet.Tables["Items"];
DataRow[]dr=this.dataTable.Select("CustomerID="+cusID.ToString());
//Thus,youhavegotalltherowswhoseCustomerIDequalstotheIDsentfromFrmCustomer
}
}
dealwi8me  Friday, January 16, 2009 8:13 AM

Hi dealwi8me,

"DataRow[] dr = this.dataTable.Select("CustomerID = " + cusID.ToString());"
This code will select all the rows whose CustomerID column = cusID. After you got the rows. you can make a new DataTable to hold these rows and bind this DataTable to the DataGridView instead of the previous one.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, January 16, 2009 9:31 AM
Kira,
I'm not sure i understood correctly what i should do.
If it's not much trouble for you can you explain your last post with code please?

Thank you again for your time!

dealwi8me  Sunday, January 18, 2009 3:23 PM
Hi dealwi8me,

The key point of this solution is to use Select method of DataTable. When you use this method, it can return the filtered rows base on your filter expression.My solution is pass the selected customer ID and the DataSet to the FrmItems,then in the FrmItems, you can selected the customer record whose CustomerID is the same as the one you have selected.That means "DataRow[] dr" only contain the rows whose customerID is "1".I don't know what problem you are facing now.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, January 19, 2009 4:20 AM

You can use google to search for other answers

Custom Search

More Threads

• CollectionTypeConverter / ArrayTypeConverter with PropertyGrid
• Form Designer nowhere to be found - new to C#
• Displaying DataGridViewColumn-derived classes in the ColumnType property of the Edit Columns dialog box
• Baseclass inherits from component
• Hide Events from Intellisense
• Button inside multiline TextBox in VB?
• Properties window keeps giving me "Object reference not set to an instance of an object." error
• C++/CLI Windows Form Designer bug in causing an event handler to disappear
• too slow
• Visual Studio is not adding a .resx with a form...