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

Creating a new row from scratch

6 Answers 190 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ludovic Gerbault
Top achievements
Rank 1
Ludovic Gerbault asked on 27 Aug 2009, 02:40 PM
Hello,

My application needs me to create a radgrid completely programmatically.

I was wondering if it is possible to create a radgrid that would be used to type in information.
I would first create all the columns I need (GridViewDataColumn and GridViewComboBoxColumn) and then add a new DataRecord to the records collection

like RadGridView.Records.Add(new DataRecord())

I tried it and it kind of work, but I can't add new rows by pressing insert, and the grid behaves weirdly (It won't exit the edit mode sometimes, or the items I choose in the comboBoxes just disappear when I exit the edit mode, and sometimes, it won't let me go into the edit mode)

Anyway, I know the radgrid is not supposed to be used like this, but does anyone thinks there might be a way to do it ?

6 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 28 Aug 2009, 04:43 AM
Hi Subileau Pascal,

You can create RadGridView in code alone but I am not sure that using RadGridView.Records.Add(new DataRecord()) is a good ideal.

What about binding the grid to an ObservableCollection and adding new items to collection instead? Will that work for your scenario?

All the best,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ludovic Gerbault
Top achievements
Rank 1
answered on 28 Aug 2009, 06:23 AM
I could try that, but don't I need to give the observablecollection a type for that to work ?

I don't have a defined type to cast the observablecollection because this particular grid can have differents structures in terms of coluimns number and type.
0
Ludovic Gerbault
Top achievements
Rank 1
answered on 28 Aug 2009, 10:55 AM
This is what I've tried. It's getting closer to what I want, but still not quite yet.

I can't type anything in yet, but I can insert new rows

ClientSilverClient _wsSilverClient = new ClientSilverClient(); 
        ObservableCollection<DataRow> collection = new ObservableCollection<DataRow>(); 
        public FormGrid(int id_societe,int id_categorie,int id_contact,string nom_conteneur) 
        { 
            InitializeComponent(); 
            _wsSilverClient.getInterfaceGrilleAsync(id_societe, id_categorie, id_contact, 0,nom_conteneur); 
            _wsSilverClient.getInterfaceGrilleCompleted += new EventHandler<getInterfaceGrilleCompletedEventArgs>(_wsSilverClient_getInterfaceGrilleCompleted); 
        } 
 
        void _wsSilverClient_getInterfaceGrilleCompleted(object sender, getInterfaceGrilleCompletedEventArgs e) 
        { 
            IList<InterfaceBO> result = e.Result; 
            foreach (InterfaceBO r in result) 
            { 
                switch (r.type_zone) 
                { 
                    case 1: GridViewDataColumn col = new GridViewDataColumn() { Header = r.lib_zone, DataType = typeof(string) }; 
                        FormGrille.Columns.Add(col); 
                        break
                    case 2: break
                    case 3: GridViewComboBoxColumn col2 = new GridViewComboBoxColumn() { Header = r.lib_zone, DataType = typeof(string) }; 
                        FormGrille.Columns.Add(col2); 
                        break
                    case 4: GridViewDataColumn col3 = new GridViewDataColumn() { Header = r.lib_zone, DataType = typeof(bool) }; 
                        FormGrille.Columns.Add(col3); 
                        break
                    case 5: break
                    case 6: GridViewDataColumn col4 = new GridViewDataColumn() { Header = r.lib_zone, DataType = typeof(DateTime) }; 
                        FormGrille.Columns.Add(col4); 
                        break
                    case 7: break
                } 
 
                 
                collection.Add(new DataRow()); 
                FormGrille.ItemsSource = collection; 
            } 
 
        } 
 
        private void FormGrille_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e) 
        { 
            DataRow row = new DataRow(); 
            e.NewObject= row; 
        } 
 
        private void FormGrille_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e) 
        { 
 
        } 
    } 

It is kind of urgent, if someone could take a look, I'd be very grateful

Ludovic
0
Ludovic Gerbault
Top achievements
Rank 1
answered on 28 Aug 2009, 03:41 PM
OK, I'm getting closer.

Right now, I can create a DataTable which structure matches what I want, and add a blank datarow in it.
Then, I create all the columns I need and bind each of them to my datatable, and finally set the itemsource.

I get a radgridview with the correct number of columns, the types I want, and a blank row.

The thing is, this first row won't fire the RowEditEndingEvent, I need to press insert, to add a second blank datarow that will this time fire the event.

Why is that ?

Second thing, when I fill in my grid with my data, the roweditendingevent is fired, I can then create a datarow, using the e.Row.Cells property to retreive the data and add it manually to the datarow.
Then, I add this row to my dataTable, and Rebind the radgrid.

The newly created row always disappear, even though the datatable has now 2 rows to display.

I just don't get what I'm doing wrong.
Can someone help me ?

Ludovic
0
Nedyalko Nikolov
Telerik team
answered on 31 Aug 2009, 07:45 AM
Hi Subileau Pascal,

Instead of RadGridView.Records.Add(), you can call RadGridView.BeginInsert() method which do the same as Insert keyboard button. In addition you can hook for the RadGridView.AddingNewDataItem event in order to create object different from default (returned by parameterless constructor).

Can you provide me with more detailed info about your case? You can open a support ticket and attach a sample project (which will be very helpful). I need some more info to investigate RadGridView.RowEditEnded (why not fired) and why data is not saved correctly.

All the best,
Nedyalko Nikolov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ludovic Gerbault
Top achievements
Rank 1
answered on 31 Aug 2009, 07:58 AM
Actually, I managed to create the behaviour I wanted combining the AddingNewDataItem and the CellEditEnded properties, which are creating and filling new datarow as I'm typing.

Thanks for your support anyway.


Tags
GridView
Asked by
Ludovic Gerbault
Top achievements
Rank 1
Answers by
Milan
Telerik team
Ludovic Gerbault
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or