I am referring the below link,
http://www.telerik.com/help/aspnet-ajax/grid-binding-description-bind-method.html
two way binding at RadGrid, and I am currently migrating the GridView to RaidGrid, which GridView Application was using bind method to DataTable and save it DataBase, You may refer the below link,
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-binding-to-datatable.aspx
and My question is,
1. ItemUpdated event was not fired, does it support? if not, any alternative solutions?
2. which event can be traced when data has been updated to the DataSource by using the above Two-way Declarative Binding approach?
3. Do we need to explicit update the value from RadGrid to DataSource (e.g DataTable)? for GridView, there is a function call gridView.UpdateRow to upate to DataSource.
4. As I have plenty of logic monitoring the dataTable dataChaning event for data validation, conditional data retrieving,calculation and so, it is painful if bind to DataTable is not working for GadGrid, Any Suggest for me?
Thanks.
12 Answers, 1 is accepted


Hello,
You can use RadGrid’s NeedDataSource event to bind RadGrid. Following is the code which shows how to bind RadGrid from code and how to perform the Update Opeartion.Also you can check this code library which shows how to perform insert/update/delete operation from code behind .
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
onneeddatasource
=
"rdgdr1_NeedDataSource"
onupdatecommand
=
"rdgdr1_UpdateCommand"
>
<
MasterTableView
DataKeyNames
=
"OrderID"
CommandItemDisplay
=
"top"
>
<
Columns
>
<
telerik:GridBoundColumn
UniqueName
=
"OrderID"
DataField
=
"OrderID"
HeaderText
=
"OrderID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"EmployeeID"
DataField
=
"EmployeeID"
HeaderText
=
"EmployeeID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"OrderDate"
DataField
=
"OrderDate"
HeaderText
=
"OrderDate"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"ShipName"
DataField
=
"ShipName"
HeaderText
=
"ShipName"
>
</
telerik:GridBoundColumn
>
<
telerik:GridEditCommandColumn
UniqueName
=
"edit"
ButtonType
=
"PushButton"
HeaderText
=
"edit"
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
public
static
string
connection = WebConfigurationManager.ConnectionStrings[
"NorthwindConnectionString3"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(connection);
public
SqlCommand SqlCommand =
new
SqlCommand();
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
string
selectQuery =
"select * from Orders"
;
SqlDataAdapter adapter =
new
SqlDataAdapter(selectQuery,conn);
DataTable dt =
new
DataTable();
conn.Open();
adapter.Fill(dt);
conn.Close();
RadGrid1.DataSource = dt;
}
protected
void
RadGrid1_UpdateCommand(
object
sender, GridCommandEventArgs e)
{
conn.Open();
GridEditableItem editItem = (GridEditableItem)e.Item;
string
orderId = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex][
"OrderID"
].ToString();
string
EmployeeID = (editItem[
"EmployeeID "
].Controls[0]
as
TextBox).Text;
string
OrderDate = (editItem[
"OrderDate"
].Controls[0]
as
TextBox).Text;
string
ShipName = (editItem[
"ShipName"
].Controls[0]
as
TextBox).Text;
string
updateQuery =
"Update Orders set EmployeeID='"
+ EmployeeID +
"',OrderDate='"
+ OrderDate +
"',ShipName='"
+ ShipName +
"' where OrderID='"
+ orderId +
"'"
;
SqlCommand.CommandText = updateQuery;
SqlCommand.Connection = conn;
SqlCommand.ExecuteNonQuery();
conn.Close();
}
Regarding your queries:-
1) 1) ItemUpdated event will fire for Declarative DataBinding only. For more information refer this link
2) 2) You can use RadGrid’s UpdateCommand event to perform update operation.
3) 3) If you are binding the grid using NeeddataSource event, you don’t need to explicitly bind the grid after the update operation
4) 4) If you want to validate the data before updating , you can use the concept mentioned in this documentation
Please elaborate your scenario more if it doesn’t help.
Thanks,
Princy.

But the concept of two-way Binding is not to manually handle the SQL itself for CRUD, for what your are showing to us, in the backend still handing the sql script for data updating and inserting. What I was using in GridView, everything is done at DataTable and using SQLDataAdapter.Update(datarow). I have no code behind to retrieve the value from grid and then build up the sql script to perfrom insert,update or delete.
And I have written my own datasource control which Inherits DataSourceControl, so resulting the same as Declarative DataBinding as you can see in the coding.
I am using needdatasource a lot, I have no problelm to retrieve the data from datasource, but the updating to the datasorce. As form my understanding, "Bind" key word should help to get it done rather like what you showing to me, in code behind to retrieve value from Grid and one by one to form a script and execute the script. In that case, the "Bind" will come to meaningless.
Help can understand my problem.

I guess you want to perform all the CRUD operation from aspx itself. If so you can follow RadGrid's Declarative DataBinding approach. I have given a sample code snippet to perform update operation using SqlDataSource. Please make sure that you have set the AllowAutomaticUpdates to true to perform update operation and to fire onitemupdated event .
Please take a look into the following code.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
DataSourceID
=
"sqlDataSource1"
AllowAutomaticUpdates
=
"true"
onitemupdated
=
"RadGrid1_ItemUpdated"
>
<
MasterTableView
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridEditCommandColumn
UniqueName
=
"EditColumn"
ButtonType
=
"ImageButton"
></
telerik:GridEditCommandColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"EmployeeID"
DataField
=
"EmployeeID"
></
telerik:GridBoundColumn
>
<
telerik:GridTemplateColumn
>
<
ItemTemplate
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text='<%#Eval("LastName")%>'></
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
Text='<%#Bind("LastName")%>'></
asp:TextBox
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
div
>
<
asp:SqlDataSource
ID
=
"SqlDataSource1"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
SelectCommand="SELECT [EmployeeID], [LastName] FROM [Employees]"
UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName WHERE [EmployeeID] = @EmployeeID" >
<
UpdateParameters
>
<
asp:Parameter
Name
=
"LastName"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"EmployeeID"
Type
=
"Int32"
/>
</
UpdateParameters
>
</
asp:SqlDataSource
>
Hope this helps.
Regards,
Princy.

As I said I am not direct update the data from grid to the database, but DataTable. My Previous Code,
ASP.NET GridView -> DBDataSource (it actually DataTable) -> SQL Database
I was writing my own DBDataSource that Inherits DataSourceControl, which intend it is a DataTable inside DataSet.
i AM Already attaching the code in the following link, which i am going to attached again here. It shows how I am use the GridView as well as How am I going to migrate to RadGrid.
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-binding-to-datatable.aspx
The question is very simple,
I am using 2-way binding approach by using 'Bind' as exactally describe in your help as following link,
http://www.telerik.com/help/aspnet-ajax/grid-binding-description-bind-method.html
Now, I am going to use same approach and migrate from GridView to RadGrid, How can it be done If I am DataTable as underly DataSourceID.
Before getting to the Bind method you need to databound RadGrid to some datasource. If it is a declarative datasource you will do it with the DataSourceID property if you are using some other type of object you will do it through the DataSource property.
Once you have databound the grid instance, the Bind method will allow extraction of the data. You could check this MSDN thread for more information on how datasource executes CRUD operations. Bind method extracts the value from the editable control and pass it to the respective parameter.
Kind regards,
Andrey
the Telerik team

And, actually I have search a lot on the forum, and it seems that 2-way "Bind" approach not working for DataTable. And now I am trying to find the alternative, i will have to accept to manually retrieve value from RadGrid and saved it the datatable, my question is, is any generic way to retrieve the value from RadGrid and save it the DataTable without handling the grid event like, "updatecommand", "insertcommand"..... e.g. when a external button is click or when a postback triggered from RadCombox which inside the RadGrid or other postback?
Thanks.

I am using my own data source control which Inherits System.Web.UI.DbdatasourceControl. and overrides GetView function as well as GetViewNames functions. and in my original asp GridView application, it declare in the aspx page like this,
<
core:DBDataSource
ID
=
"DBDataSource1"
runat
=
"server"
ObjectName
=
"SalesReturnHeader"
TableType
=
"Detail"
TableName
=
"SalesReturnHeader"
/>
the above data source was not configured CRUD sql script to database as the actually data source is DataTable.
And it is working well for the GridView, which been declared like this,
<
asp:GridView
ID
=
"SalesReturnDetailGrid"
runat
=
"server"
DataMember
=
"SalesReturnDetail"
DataSourceID
=
"DBDataSource1"
AutoGenerateColumns
=
"False"
<BR>
DataKeyNames="CompanyID,DivisionID,DepartmentID,SalesReturnNumber,SalesReturnLineNumber,ItemID"><
BR
>
<
Columns
>
and When I am trying to migrate to RadGrid, if without any changes like what declare below,
<telerik
:RadGrid
ID
=
"SalesReturnDetailGrid"
runat
=
"server"
DataMember
=
"SalesReturnDetail"
DataSourceID
=
"DBDataSource1"
AutoGenerateColumns
=
"False"
<BR>
DataKeyNames="CompanyID,DivisionID,DepartmentID,SalesReturnNumber,SalesReturnLineNumber,ItemID"><
BR
>
<
Columns
>
I am able to retrieve the value from the data source, but come to the updating/inserting, can not proceed. And at same time I have tried the following approach (Advancing binding by using NeedDataSource),
If Not e.IsFromDetailTable Then
RadGridDOHeader.DataSource = DBDataSource1
RadGridDOHeader.DataMember = "SalesReturnDetail"
End If
End Sub
Same thing, data retrieving no problem, but no idea how to perform updating/inserting.
if can help on my solutions.
The reason that I have sent you the article is that when you are creating custom datasource controls you need to ensure that you have the Update/Insert/Selecte/Delete parameters collection and you have defined the behavior that will use these parameters.
Then you need to fill the parameters in the respective collection. After that it is all up to the datasource what will be the behavior.
Unless you need to use this custom datasource in many places I think is more appropriate to implement the manual CRUD operations. With minor changes they will be easily used in other scenarios as well.
Kind regards,
Andrey
the Telerik team

You could modify the DataTable on any event you like, however if the Grid relies on this DataTable you will need to Rebind the grid manually after the modification. This MSDN thread describes how to edit DataTable rows.
If you want to update the datatable through RadGrid you need to use the Update/Insert/Delete commands events. You could check this help for more information about this approach.
Greetings,
Andrey
the Telerik team
