This is a migrated thread and some comments may be shown as answers.

Add empty row to Rad Grid

1 Answer 222 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ahmad
Top achievements
Rank 1
ahmad asked on 18 Feb 2009, 02:18 PM
Hi Experts ..

I am using LINQ to to set RadGrid.DataSource, and I want to add additional empty row, at the 1st row of the grid, how to do that?

Master db = new Master(ConfigurationManager.ConnectionStrings["master"].ConnectionString); 
db.Log = Console.Out; 
EventsGrid.DataSource = from p in db.Event select p; 

<telerik:RadGrid ID="EventsGrid" runat="server" AllowSorting=true AllowPaging=true
</telerik:RadGrid> 

any idea?

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 19 Feb 2009, 06:22 AM
Hi,

A suggestion would be to convert your collection   to a data table uisng the LINQToDataTable function  as shown in the code snippet below . Add a new row to the datatable  on some button click and then set the data table as the source of your grid.
CS:
   RadGrid1.DataSource = LINQToDataTable(from p in db.Event select p);
 
     public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist) 
        { 
            DataTable dtReturn = new DataTable(); 
 
            // column names  
 
            PropertyInfo[] oProps = null
 
            if (varlist == null) return dtReturn; 
 
            foreach (T rec in varlist) 
            { 
                // Use reflection to get property names, to create table, Only first time, others  
                // will follow  
                if (oProps == null) 
                { 
                    oProps = ((Type)rec.GetType()).GetProperties(); 
                    foreach (PropertyInfo pi in oProps) 
                    { 
                        Type colType = pi.PropertyType; 
 
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() 
                        == typeof(Nullable<>))) 
                        { 
                            colTypecolType = colType.GetGenericArguments()[0]; 
                        } 
 
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType)); 
                    } 
                } 
 
                DataRow dr = dtReturn.NewRow(); 
 
                foreach (PropertyInfo pi in oProps) 
                { 
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue 
                    (rec, null); 
                } 
 
                dtReturn.Rows.Add(dr); 
            } 
            return dtReturn; 
        } 


Thanks,
Princy
Tags
Grid
Asked by
ahmad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or