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
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:
- http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/manual-crud-operations/defaultcs.aspx
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/update-records/updating-values-using-inplace-and-editforms-modes
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/insert-records/inserting-values-using-inplace-and-editforms-modes
Regards,
Viktor Tachev
Telerik
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!
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
Thank you!
But if there is a way to know which column is a primary key column?
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