in
Forums
Blogs
Files
Devexpress.Com
Client Center
Support Center
DevExpress Channel

How to bind Master-Detail data source in gridview programmatically?

Last post 11/25/2008 8:25 PM by qiux sense. 10 replies.
Page 1 of 1 (11 items)
Sort Posts:
Previous Next
  • 11/18/2008 9:13 PM

    How to bind Master-Detail data source in gridview programmatically?

    Hi,

    I need help for above issue, I need to bind a master-detail relationship into a gridview...

     

  • 11/19/2008 4:11 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

     I know how to do it when the content of the data is known ahead of time.

    Design your GridControl with two related GridView objects.  The outer view will contain the "master" data and the inner view will contain the "detail" data.  Design each view with appropriate grid columns for the data you want to fetch and display.  Provide a suitable label, like "Details", for the RelationName.

    In a location that you find suitable to fetch and display the data, use the following code.  Replace the invocations of FetchMasterData() and FetchDetailData() with your own methodology for obtaining the data for each view.  Replace "MasterID" with your own column name that represents the relationship between your two query results.  Replace "Details" with the RelationName you provided in the GridControl designer.
    __________

    DataTable masterTable = FetchMasterData();
    DataTable detailTable = FetchDetailData();

    DataSet dataSet = new DataSet();
    dataSet.Tables.Add( masterTable );
    dataSet.Tables.Add( detailTable );
    dataSet.Relations.Add( "Details", masterTable.Columns[ "MasterID" ], detailTable.Columns[ "MasterID" ] );

    DataViewManager dvm = new DataViewManager( dataSet );
    DataView dv = dvm.CreateDataView( masterTable );
    gridControl1.DataSource = dv;
    __________

  • 11/19/2008 8:12 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    Hi, Thanks for the reply, I will try it :)

    but another question is "Design each view with appropriate grid columns for the data you want to fetch and display"

    Did you mean that I have to set the columns for the view? If we bind the datatable to the view, the columns will be shown automatically, is it correct? pls correct me if I'm wrong.. :)

  • 11/19/2008 9:07 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    If you know what data you will be fetching ahead of time, you may use the GridControl designer to manually create the GridColumns for each GridView.  If you are looking for something truly dynamic, to support arbitrary queries of two or more data sources that happen to be related, and where the columns are not known at design time, that's a bit more work.  However, just like any other object, you might try creating the GridControl, each GridView, and every GridColumn at run time to fit the data that you want to display.  I just haven't tried that yet.
     
    As for the automatic creation and binding of columns, that works when you have AutoPopulateColumns set to True or when you invoke the PopulateColumns method.  Then the run-time creation of GridColumns is handled for you.  You still need enough information at run-time to create the necessary LevelTree information for the relationship between the master and detail views.
     
    Also check out the following help topic for another example.
     
  • 11/24/2008 6:26 AM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    Hi, I found below error, please  help me to solve it, :)

    error: DataTable already belongs to another DataSet.

    clsOne

    Public function LoadDataMaster() as datatable

        Dim ds As New DataSet
            Dim dt As DataTable = ds.Tables.Add("tblData")
            Dim da As MySqlDataAdapter
        Dim str As String
            str = "SELECT EmployeeID, EmployeeName FROM tblemployee"
            da = New MySqlDataAdapter(str, Connection.conn)
           
            Open()
            da.Fill(ds, "tblData")
            Close()
      
            Return ds.tables("tblData")

    end function

     

    Public function LoadDataDetail() as datatable

        Dim ds As New DataSet
            Dim dt As DataTable = ds.Tables.Add("tblData")
            Dim da As MySqlDataAdapter
        Dim str As String
            str = "SELECT DependentID, DependentName FROM tbldependent"
            da = New MySqlDataAdapter(str, Connection.conn)
           
            Open()
            da.Fill(ds, "tblData")
            Close()
      
            Return ds.tables("tblData")

    end function

    FormLoad

      Dim masterdt As DataTable = Employee.LoadDataMaster()
            Dim detaildt As DataTable = Employee.LoadDataDetail()
            Dim ds1 As DataSet = New DataSet
            ds1.Tables.Add(masterdt)
            ds1.Tables.Add(detaildt)
            ds1.Relations.Add("Details", masterdt.Columns("EmployeeID"), detaildt.Columns("EmployeeID"))

            Dim dvm As DataViewManager = New DataViewManager(ds1)
            Dim dv As DataView = dvm.CreateDataView(masterdt)

            GridControl1.DataSource = dv

     

     

  • 11/24/2008 10:47 AM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    I think the error is in the name of each DataTable.  It is the same "tblData" for both the master and the detail.  Try naming them differently.
     
  • 11/24/2008 8:52 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    Already, but still got no luck, got any options or ways to do it?

    Still the same error... :)

  • 11/24/2008 9:36 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    I should have spotted this before.  You are creating a DataTable and adding it to a DataSet, variable named "ds".  Then you are trying to add the same DataTable to another DataSet, variable named "ds1".  Thus, the error message.
     
    Does the MySqlDataAdapter have a Fill method variation that accepts a DataTable as a parameter instead of a DataSet?  I know that the DbDataAdapter abstract class has such a method.
     
    Otherwise, rearrange all of your logic to fill a single DataSet with the two DataTable objects, then set your relationship item within that.  It might look something like this (assuming I can read VB correctly, being a C# developer!!) :
    __________
     
        Dim dataSet1 As New DataSet
        Dim masterdt As DataTable = ds.Tables.Add("Employee")
        Dim detaildt As DataTable = ds.Tables.Add("Dependent")
     
        Dim str As String
        Dim da As MySqlDataAdapter
     
        str = "SELECT EmployeeID, EmployeeName FROM tblemployee"
        da = New MySqlDataAdapter(str, Connection.conn)
     
        Open()
        da.Fill(dataSet1, "Employee")
        Close()
     
        str = "SELECT DependentID, DependentName FROM tbldependent"
        da = New MySqlDataAdapter(str, Connection.conn)
     
        Open()
        da.Fill(dataSet1, "Dependent")
        Close()
     
        ' The following is almost right.  Did not fetch EmployeeID from tbldependent,
        ' and do not know what the relationship should actually be.
        dataSet1.Relations.Add("Details", masterdt.Columns("EmployeeID"), detaildt.Columns("EmployeeID"))
     
        Dim dvm As DataViewManager = New DataViewManager(dataSet1)
        Dim dv As DataView = dvm.CreateDataView(masterdt)
     
        GridControl1.DataSource = dv
    __________
     
    I hope that helps.
  • 11/24/2008 10:06 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    Hi, Thanks again for the reply.

    I have tried above codes, but still got the error at here,

    'column' argument cannot be null. Parameter name: column -> pointed to  dataSet1.Relations.Add("Details", masterdt.Columns("EmployeeID"), detaildt.Columns("EmployeeID"))

    I think that doesn't have data bound... that is so annoying can't solve it, :) btw thanks to you.. Does it really has no other options? Or do you have any samples?

    Below is the code,

            Dim dataSet1 As New DataSet
            Dim masterdt As DataTable = dataSet1.Tables.Add("Employee")
            Dim detaildt As DataTable = dataSet1.Tables.Add("Dependent")

            Dim str As String
            Dim da As MySqlDataAdapter

            str = "SELECT EmployeeID, EmployeeName FROM tblemployee"
            da = New MySqlDataAdapter(str, Connection.conn)

            Connection.Open()
            da.Fill(dataSet1, "Employee")
            Connection.Close()

            str = "SELECT DependentID, DependentName FROM tbldependent"
            da = New MySqlDataAdapter(str, Connection.conn)

            Connection.Open()
            da.Fill(dataSet1, "Dependent")
            Connection.Close()

            dataSet1.Relations.Add("Details", masterdt.Columns("EmployeeID"), detaildt.Columns("EmployeeID"))

            Dim dvm As DataViewManager = New DataViewManager(dataSet1)
            Dim dv As DataView = dvm.CreateDataView(masterdt)

            GridControl1.DataSource = dv

     

  • 11/25/2008 9:24 AM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    You might see in my previous post that I had added a comment regarding the reference to a column that was not actually fetched from the tbldependent source.  You are referring to the "EmployeeID" column in the DataSet.Relations for both tables but only the master query result has a column with that name.
     
    Another master-detail sample is, of course, in the DevExpress documentation:
     
  • 11/25/2008 8:25 PM In reply to

    Re: How to bind Master-Detail data source in gridview programmatically?

    Hi, Thanks, after reviewed provided tutorials, I realized that we need 2 data adapter to fill the dataset.

    I have done it bro... thanks a lot :)

     

Page 1 of 1 (11 items)
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED