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

Update auto generated RadGrid in database

5 Answers 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marie
Top achievements
Rank 1
Marie asked on 28 Dec 2015, 05:52 PM

Hello!

 

I have created an auto generated RadGrid control.

I've found a solution for implementing an INSERT event with the help of RadGrid1.MasterTableView.AutoGeneratedColumns collection and  IGridColumnEditor.

But how should I implement an UPDATE event? How can I write update statement(for databes update) without knowing the names of the primary key columns?

 

Thank you!

5 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 29 Dec 2015, 02:41 PM
Hi Marie,

In order to handle inserting and updating data manually you need to use manual CRUD operations. You need to handle the OnInsertCommand and OnUpdateCommand events for insert and update operations respectively.

Check out the following resources that illustrate the approach:




Regards,
Viktor Tachev
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Marie
Top achievements
Rank 1
answered on 29 Dec 2015, 04:44 PM

Hi Victor,

thanks a lot for your answer!

Maybe I formulated not correctly. I have a grid with auto generated columns (the tables can change and correspondingly the column names).  My problem is that I don't know how to write an update to the sql database, because I don't know the names of columns with primary keys. I have tried to extract the names of primary columns with grid.MasterTableView.DataKeyNames[0], but somehow grid.MasterTableView.DataKeyNames.Length returned 0. 

So I would like to narrow my question to how to extract the column names of columns with primary key from the automatically generated RadGrid?

 

Thanks a lot again!

 

 

 

0
Viktor Tachev
Telerik team
answered on 30 Dec 2015, 11:35 AM
Hi,

In order to get the names of the fields you can iterate through the AutoGeneratedColumns collection of the grid.


protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridColumn[] gridColumns = RadGrid1.MasterTableView.AutoGeneratedColumns;
 
    foreach (GridBoundColumn column in gridColumns)
    {
        string dataField = column.DataField;
 
       
    }

// add updating logic here
}




Regards,
Viktor Tachev
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Marie
Top achievements
Rank 1
answered on 30 Dec 2015, 12:59 PM

Thank you!

 

But if there is a way to know which column is a primary key column?

0
Konstantin Dikov
Telerik team
answered on 01 Jan 2016, 01:37 PM
Hello Marie,

The RadGrid does not provide functionality that will determine the primary keys in your data table, because it works with the data that is provided to it (either manually or through a DataSource control). This means that in order for the update to work correctly you need to set the primary key to the DataKeyNames collection and configure your DataSource accordingly.

Notwithstanding, you could take a look at the following forum thread, where some ideas on how to make a query that will return the primary key name are suggested:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Marie
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Marie
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or