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

Gridview updating Issue

4 Answers 175 Views
GridView
This is a migrated thread and some comments may be shown as answers.
jerry
Top achievements
Rank 1
jerry asked on 19 Feb 2010, 02:26 AM
I have a gridview populated using a datatable.  Within my gridview I have 2 buttons "Up" and "Down".  The "Up" button takes the selected row and moves it up one row.  The "Down" button takes the selected row down a row.  The way i'm doing this is updating a column called "Order" and having my grid sorted by Page and Order columns.  If going up I swap order values within the grid with the row above the selected row and the opposite for down.   I have to be able to insert, delete and move rows up and down.  I'm trying to keep track of the grid rowindex and update that cell.
Ex.
Row# Page Order Text
1 1 10 Test1
2 1 20 Test2
3 1 30 Test3
Say I move row# 2 up now It should look like this
1 1 10 Test2
2 1 20 Test1
3 1 30 Test3
My problem is if I try and add a new row between rows#1-2 I should have 
1 1 10 Test2
2 1 20 Test4
3 1 30 Test1
4 1 40 Test3

Should I be updating my datatable?  I'm updating my grid.  Here is my RowChanging event
private void gv_RowsChanging(object sender, GridViewCollectionChangingEventArgs e)  
    {  
      if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)  
      {  
        RowSetup = e.NewItems[0] as GridViewDataRowInfo;  
 
        RowSetup.Cells["ID"].Value = gv.Rows[e.NewStartingIndex - 1].Cells["ID"].Value;  
        RowSetup.Cells["PAGE"].Value = gv.Rows[e.NewStartingIndex - 1].Cells["PAGE"].Value;  
        RowSetup.Cells["ORDER"].Value = Convert.ToInt32(gv.Rows[e.NewStartingIndex - 1].Cells["ORDER"].Value) + Convert.ToDecimal(".5");  
          
        foreach (GridViewRowInfo dri in gv.Rows)  
        {  
          if (dri.Cells["PAGE"].Value.ToString() == RowSetup.Cells["PAGE"].Value.ToString())  
          {  
            if (Convert.ToDecimal(dri.Cells["ORDER"].Value) > Convert.ToDecimal(RowSetup.Cells["ORDER"].Value))  
            {  
              dri.Cells["ORDER"].Value = Convert.ToInt32(dri.Cells["ORDER"].Value) + 1;  
            }  
          }  
        }  
        RowSetup.Cells["ORDER"].Value = Convert.ToDecimal(gv.Rows[e.NewStartingIndex].Cells["ORDER"].Value) + Convert.ToDecimal(".5");  
 
      }  
    } 

Thank you for any light you can shed on this.








4 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 19 Feb 2010, 08:53 AM
Hello jerry,

You do not need to update the datatable. When you change a cell value of grid row, you will affect also the data source. In your case you will change the values of the rows in the data table through the grid.

You can see the affected data table rows using GetChanges() method.

DataTable changes = yourDataTable.GetChanges();

Do not hesitate to contact us back, if you need further assistance.

All the best,
Svett
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
jerry
Top achievements
Rank 1
answered on 19 Feb 2010, 01:19 PM
Svett,
How can I add a blank row to the grid? 
When I tried
gv.Rows.Insert(rownum, dr);
I got "System.InvalidOperationException: Operation is valid only for unbound RadGridView"
Is there no InsertAt function of the gridview?

Thank you for the quick response this is a major issue in my application

Jerry
0
Elango S
Top achievements
Rank 1
answered on 22 Feb 2010, 07:34 AM
Hi,

In my rad grid my first cell should only support numbers... Wat can i do? Is there any property available ?

Reply with the solution anyone... 
0
Svett
Telerik team
answered on 22 Feb 2010, 10:24 AM
Hello Elango S,

The reason that you receive the exception is because you cannot add programatically a row in the rows collection when the grid is in unbound mode.

You can create a record in your data source, which automatically will affect the rows collection in the grid. So if you have a DataTable as data source of the grid, you should create DataRow object and add it in the data table rows collection. If the data source is a collection of custom objects, you should create a new custom object and add it in the collection.

Regarding your second question, the allowed input values for a cell depend on your data source and the defined grid column. So if you have a string column in the data source the grid will generate GridViewTextBoxColumn column. Can you give us a code snippet that shows how you define and use RadGridView control?

Kind regards,
Svett
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
jerry
Top achievements
Rank 1
Answers by
Svett
Telerik team
jerry
Top achievements
Rank 1
Elango S
Top achievements
Rank 1
Share this question
or