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

[Solved] how to build a gridview at runtime,

3 Answers 202 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
reema
Top achievements
Rank 1
reema asked on 19 Mar 2009, 05:58 AM

hi,

In my application I need to build and fill a gridview dynamically at runtime,

For columns I used the bellow code and it works. Is it possible to add rows using the same way?



Telerik.Windows.Controls.GridViewColumn[] columns;

columns = new Telerik.Windows.Controls.GridViewColumn[record.length];          

  for (int i =0; i < record.length; i++)

  {

   columns[i] = new Telerik.Windows.Controls.GridViewColumn();

   columns[i].HeaderText = record[i].record;                 

   grid.Columns.Add(columns[i]);       

   }


Thx in advance…

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 20 Mar 2009, 11:12 AM
Hello reema,

Yes it is possible to add rows runtime . What you need to do is to bind the grid initially to an empty

ObservableCollection

<Record> initially when you create the gird. Later anytime when you add a record to the observable collection, the grid will automatically add and display the newly added item.


*Record here is the type of your record.

Alternatively you may use other type of collection List<Record> for example. However when the collection is not capable of item changes notifications you will need to manually call the Rebind() method of the RadGridView after adding new data.


Kind regards,
Pavel Pavlov
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Atiq Ur Rehman
Top achievements
Rank 1
answered on 24 Mar 2009, 11:31 AM
hi i need some help on this can you please help me with some code?
0
Vlad
Telerik team
answered on 24 Mar 2009, 04:02 PM
Hello Atiq Ur Rehman,

Here is an example:
namespace SilverlightApplication1 
    public partial class Page : UserControl 
    { 
        public Page() 
        { 
            InitializeComponent(); 
 
            var data = from i in Enumerable.Range(0, 10) 
                                 select new MyObject() { ID = iName=String.Format("Name{0}", i)}; 
 
            RadGridView1.ItemsSource = data
        } 
    } 
 
    public class MyObject 
    { 
        public int ID { get; set; } 
        public string Name { get; set; } 
    } 
 


Sincerely yours,
Vlad
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
reema
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Atiq Ur Rehman
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or