locked
how to assign DataRelation dataset as a source to a gridview RRS feed

  • Question

  • User1433899244 posted

    HI there,

    i am using DataRelation to join two datatables. here is the code.

               Dim dt1 As New DataTable
                dt1.Load(dr1)
                Dim dt2 As New DataTable
                dt2.Load(dr2)
    
                Dim datarelation As DataRelation
                Dim dc1 As DataColumn
                Dim dc2 As DataColumn
                Dim ds As New DataSet
    
                ds.Tables.Add(dt1)
                ds.Tables.Add(dt2)
                dc1 = dt1.Columns("CL_R")
                dc2 = dt2.Columns("CL_R")
    
                datarelation = New DataRelation("JoinData", dc1, dc2)
                ds.Relations.Add(datarelation)
    
                NewDg.DataSource = ds
                NewDg.DataBind()


    I would like to display both the tables, dt1 and dt2 in the datagrid, NewDg.
    Now it dispalys only the data of datatable dt1 as this is parent table.
    I want to dispaly both the parent table and child table. please help me. thank you

    Tuesday, June 28, 2011 5:16 PM

Answers

  • User3866881 posted

    @decker,

    The appilcation was working fine till yesterday, now it gives me the error,I am not sure why. I did not change anything. I left the code as it is on weekend and today after coming back, it gives error. could you please help me. thank you.

    Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Dec imal,VB$AnonymousType_0`10[System.Decimal,System.DateTime,System.DateTime,Sys tem.DateTime,System.DateTime,System.Decimal,System .Decimal,System.String,System.String,System.String]]' to type 'System.Data.DataSet'."

    Hello:)

    According to the err description, it seems that you are wanting to convert from an anoymous class instance to a DataSet, isn't it?

    My suggestion at present is that you should directly bind the anoymous class instance to the data presentation control or create a DataSet and use for each to loop each item in the anoymous class instance and assign to the DataSet one by one.

    Thx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 5, 2011 9:06 PM

All replies

  • User-269404413 posted

    here is an related article

    Wednesday, June 29, 2011 8:28 AM
  • User1433899244 posted

    hi, thank you for your reply. But that did not help me much. I have finished pretty much what was in that article.

    I have issue getting the child table contents and parent table contents into one table.

    Could you please let me know if there is a way to join two data tables.

    I tried using LINQ but I am getting error.

     Dim dt1 As New DataTable
                dt1.Load(dr1)
                 Dim dt2 As New DataTable
                 dt2.Load(dr2)
      
      Dim joinquery = From t1 In dt1.AsEnumerable(), t2 In dt2.AsEnumerable() _
                     Where (t1.Field(Of Long)("CL_R") = _
                         t2.Field(Of Long)("CL_R")) _
                     Select t1, t2
     NewDg.DataSource = joinquery
                 NewDg.DataBind()
     

    When I debug this, I get the error, System.InvalidCastException: Specified cast is not valid.
    I get this error in the line, joinquery =...
    I also have used Integer as the field type for the column, CLASS_NO.

    thank you

    Wednesday, June 29, 2011 3:14 PM
  • User3866881 posted

    Dim joinquery = From t1 In dt1.AsEnumerable(), t2 In dt2.AsEnumerable() _ Where (t1.Field(Of Long)("CL_R") = _ t2.Field(Of Long)("CL_R")) _ Select t1, t2 NewDg.DataSource = joinquery NewDg.DataBind()

    Hello, you can change to this——

    Dim joinquery = From t1 In dt1.AsEnumerable(),
                           From t2 in dt2.AsEnumerable(), _
                           Where t1["FieldName"].ToString()==t2["FieldName"].ToString()
                           Select New With
                            {
                                 Key .NewField = t1["FieldName1"],
                                 …………………………

                            }
                    
    NewDg.DataSource = joinquery
                
    NewDg.DataBind()

    Wednesday, June 29, 2011 10:39 PM
  • User1433899244 posted

    thank you for your reply.

    I get this new error,

    System.Web.HttpException: The data source for GridView with id '' did not have any properties or attributes from which to generate columns.  Ensure that your data source has content.

    I checked for the content in joinquery and it has the rows of the two tables, t1 and t2. So I am not sure why it is not binding the data.

    thank you

    Thursday, June 30, 2011 11:13 AM
  • User3866881 posted

    Hello again:)

    Sorry……

    Please try to convert each of the field's value into a right type——Sample:

     Key .NewField = t1.Field(Of Long)("FieldName")


     

    Thursday, June 30, 2011 8:56 PM
  • User1433899244 posted

    thank you....that did work...

    thanks once agian..

    Tuesday, July 5, 2011 11:09 AM
  • User1433899244 posted

    @decker,

    The appilcation was working fine till yesterday, now it gives me the error,I am not sure why. I did not change anything. I left the code as it is on weekend and today after coming back, it gives error. could you please help me. thank you.

    Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Dec imal,VB$AnonymousType_0`10[System.Decimal,System.DateTime,System.DateTime,Sys tem.DateTime,System.DateTime,System.Decimal,System .Decimal,System.String,System.String,System.String]]' to type 'System.Data.DataSet'."

    Tuesday, July 5, 2011 2:42 PM
  • User3866881 posted

    @decker,

    The appilcation was working fine till yesterday, now it gives me the error,I am not sure why. I did not change anything. I left the code as it is on weekend and today after coming back, it gives error. could you please help me. thank you.

    Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Dec imal,VB$AnonymousType_0`10[System.Decimal,System.DateTime,System.DateTime,Sys tem.DateTime,System.DateTime,System.Decimal,System .Decimal,System.String,System.String,System.String]]' to type 'System.Data.DataSet'."

    Hello:)

    According to the err description, it seems that you are wanting to convert from an anoymous class instance to a DataSet, isn't it?

    My suggestion at present is that you should directly bind the anoymous class instance to the data presentation control or create a DataSet and use for each to loop each item in the anoymous class instance and assign to the DataSet one by one.

    Thx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 5, 2011 9:06 PM
  • User1433899244 posted

    thank you Decker..that helped..sorry the code was huge and could not post it....

    Wednesday, July 6, 2011 10:08 AM