[SOLVED] Developer Express Grid and GetDataRow

Posts   
 
    
johnh
User
Posts: 21
Joined: 28-Sep-2003
# Posted on: 20-Dec-2004 23:56:14   

With the Developer Express Grid, the latest LLBLGen (20Dec) and the Firebird database

populating the grid:


    BoatCollection bc = new BoatCollection();
    bc.GetMulti(null);
    grdBoats.DataSource = bc;

retrieving the current row (doesn't work - gives undefined value for dr


                        if (gvBoats.SelectedRowsCount > 0)
            {
                DataRow dr  = gvBoats.GetDataRow(gvBoats.GetSelectedRows()[0]);
                int id = int.Parse(dr["BtId"].ToString());
                if(new DlgBoatEdit(id).ShowDialog() == DialogResult.OK)
                {
                    LoadBoats();
                }
            }           

however if I use GetMultiAsDataTable to populate the grid, the items don't display in the grid, but the selection of the current row (above code) does.


                        BoatCollection bc = new BoatCollection();
            grdBoats.DataSource = BoatCollection.GetMultiAsDataTable(null,0,null);

I have endeavoured to get around the problem by creating a (generic) CollectionToDataTable method


private DataTable CollectionToDataTable(EntityCollectionBase collection, EntityBase entity)
        {
            DataTable dt = new DataTable();
            for (int i=0; i<entity.Fields.Count;i++)
            {
                dt.Columns.Add(entity.Fields[i].Name, entity.Fields[i].DataType);
            }
            foreach(EntityBase eb in collection)
            {
                DataRow row = dt.NewRow();
                for (int i=0; i<entity.Fields.Count;i++)
                {
                    row[eb.Fields[i].Name] = eb.Fields[i].CurrentValue;
                }
                dt.Rows.Add(row);
            }
            return dt;

    
        }

And this now seems to work


            BoatCollection bc = new BoatCollection();
            BoatEntity boat = new BoatEntity();
            bc.GetMulti(null);
            grdBoats.DataSource = CollectionToDataTable(bc,boat);

My first question - is my CollectionToDataTable method safe to use? Secondly - do you have any idea why this is causing a problem?

johnh
User
Posts: 21
Joined: 28-Sep-2003
# Posted on: 21-Dec-2004 03:33:59   

I guess it answers itself - you use GetRow and cast the return back to the underlying entity type


    if (gvBoats.SelectedRowsCount > 0)
            {
                BoatEntity be = (BoatEntity)gvBoats.GetRow(gvBoats.GetSelectedRows()[0]);
                if(new DlgBoatEdit(be.BtId).ShowDialog() == DialogResult.OK)
                {
                    LoadBoats();
                }
                
            }       

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 21-Dec-2004 10:22:21   

GetMultiAsDataTable() fetches the data in a datatable, so this should always work (it doesn't convert), at least it should give you a datatable with the data. You could test if there is actually any data in the datatable.

What can be wrong is that the grid doesn't want to work with a datatable but with a dataset.

Frans Bouma | Lead developer LLBLGen Pro