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

grid.Rows.Add()

4 Answers 124 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sandrino
Top achievements
Rank 1
Sandrino asked on 23 Oct 2008, 01:56 PM
Dear community,

We would like to create a timesheet page using the grid. The grid would have 5 columns where people can enter information, and by default also have 5 rows.

If needed people should have to be able to click 'Add' to add a new row where they can enter new information.

The problem is that with standard controls (like DGV, Table) it's possible to perform a Rows.Add(). Am I mistaken or is the RadGrid databound only?

Is it possible to add rows in code without datasource? (like grid.Rows.Add())

Regards

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 23 Oct 2008, 02:20 PM
Hello Sandrino,

RadGrid is very similar to standard DataGrid and GridView. To achieve your goal you can assign DataSource and call DataBind(). Adding rows is not possible exactly in the same way as in DataGrid and GridView controls.

Sincerely yours,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Boone
Top achievements
Rank 2
answered on 18 Feb 2009, 05:32 PM
Is this still the case or can you now add rows to the grid in code behind with the new releases?
0
Princy
Top achievements
Rank 2
answered on 19 Feb 2009, 07:16 AM
Hello Boone,

A suggestion would be as follows: you can create a new DataTable and add columns to it in the PageLoad event. Then in the NeedDataSource event of the grid add rows to the DataTable and save it in a session variable which should be bound to the grid. Then on button click you can add more rows to the DataTable and rebind the grid to the new DataTable. The following code should help you understand better :

 public DataTable dt;  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            dtnew DataTable();  
            dt.Columns.Add("Column");      
  
        }  
  
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            if (!IsPostBack)  
            {                
                dt=  AddRow(dt);  // call the method to create row 
                ViewState["dt"] = dt;  
            }  
            dt = (DataTable)ViewState["dt"];  
            RadGrid1.DataSource = dt;  
        }  
  
        private DataTable AddRow(DataTable dt) 
        { // method to create row 
            DataRow dr = dt.NewRow();  
            dr[0] = "";  
            
            dt.Rows.Add(dr);  
            return dt;  
        }  
  
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            DataTable dt = (DataTable)ViewState["dt"];  
            ViewState["dt"] = AddRow(dt);  
            RadGrid1.Rebind();  
                         
        }  

Hope this helps..
Princy.
0
Boone
Top achievements
Rank 2
answered on 19 Feb 2009, 03:06 PM
That is what I'm doing now Princy, just didn't like putting 2000 records in a session for 350,000 customers. I just got a reply on a support ticket from Telerik telling me that it's not possible to do what I want.
Tags
Grid
Asked by
Sandrino
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Boone
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or