NHIbernate -.NET-Binding IList to GridView in .Net 2.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhivya1
    New Member
    • Feb 2008
    • 5

    NHIbernate -.NET-Binding IList to GridView in .Net 2.0

    Hi,

    Is it possible to get the results of HQL queries in DataSet?.I want to display results of HQL query to GridView. As HQL results are returned in the form of IList/List collections i am unable to Bind to Grid View.

    pls let me know,

    1. How to bind IList collections to Grid view?
    eg:
    IList results=Query.L ist()
    GridView1.DataS ource=results;
    GridView.DataBi nd();

    if i run the above code it will display the result in Grid only if i have the result returned as only 1 row.If more than one row is returned as a result of executing HQL query, it returns the properties of IList in the GridView..
    any solution for this problem??
    (or)

    2 . Is it possible to return the result of HQL query directly in the Form of DataSet?

    thanks in Advance,
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here is an article that may help:
    Hibernate Query Language (HQL)

    Comment

    • dhivya1
      New Member
      • Feb 2008
      • 5

      #3
      Originally posted by kenobewan
      Here is an article that may help:
      Hibernate Query Language (HQL)

      Thanks for the reply.
      but my question is: HQL returns an Ilist..that is fine.But how do I bind the IList to Gridview?
      Is there any method of GridView DataBinding with IList? or
      Is there any other method of executing HQL queries other than List and IList so that i can directly get the results in the form of DataSet/DataTable?

      Thanks,

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        That would normally be the datasource of the gridview. HTH.

        Comment

        • tomaandtoma
          New Member
          • Sep 2008
          • 1

          #5
          Ciao,

          This is the code that I always use in order to bind a IList returned by Nhiberbate to a DataGridView:

          Code:
           private BindingSource bs = new BindingSource();
                  gridView.DataSource = bs;
          ......................................................................................................
                  IList<MyType> myList= criteria.List<MyType>();
                  bs.DataSource=myList;
                  bs.ResetBindings(false);
          You first create a BindingSource object that you link to the DataSource of your grid. After that, when you have retreived you IList (here I use ICriteria but is the same if you use HQL to retreive the IList) you link the DataSource of the BindingSource to the IList and everytime you change the data in the IList the only thing you need to call is bs.ResetBinding s(false)

          Hope it helps!
          Last edited by PsychoCoder; May 22 '12, 04:15 PM. Reason: Added code tags

          Comment

          • sriharshakollur
            New Member
            • May 2012
            • 1

            #6
            Code:
            ISQLQuery newQuery = session.CreateSQLQuery("select * from Test_Persons comp,Test_ProductOrder ORD WHERE comp.custId= ORD.CustomerID");
                        IList list1 = (IList)newQuery.List();
                        DataTable tab = new DataTable();
                        for (int i = 0; i < columncount; i++)
                            tab.Columns.Add();
                        for (int i = 0; i < list1.Count; i++)
                        {
                            Object[] newobj = (Object[])list1[i];
                            tab.Rows.Add(newobj);
                        }
                        GridView1.DataSource = tab;
                        GridView1.DataBind();
            -0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0

            Hope the code helps
            Last edited by PsychoCoder; May 22 '12, 04:15 PM. Reason: Code tags added

            Comment

            Working...