I am creating a grid dynamically and its datasource is a datatable. I am not using need data source event since I am creating the grid dynamically.
I cannot insert an item or update an item. The datasource does not reflect the changes.
Also, when I click on 'Insert' link the grid remains in edit mode.
Can someone tell me how to insert and update to an im-memory datatable ( not database)?
Thanks
I cannot insert an item or update an item. The datasource does not reflect the changes.
Also, when I click on 'Insert' link the grid remains in edit mode.
Can someone tell me how to insert and update to an im-memory datatable ( not database)?
Thanks
6 Answers, 1 is accepted
0

Princy
Top achievements
Rank 2
answered on 29 Dec 2010, 06:25 AM
Hello Sunil,
Here is a sample application which shows how to insert and update data into datatable.
ASPX:
C#:
Thanks,
Princy.
Here is a sample application which shows how to insert and update data into datatable.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"true"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
>
<
MasterTableView
ShowHeadersWhenNoRecords
=
"true"
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridEditCommandColumn
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
DataTable dtValues;
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
RadGrid1_InsertCommand(
object
sender, GridCommandEventArgs e)
{
GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
TextBox txt1 = (TextBox)insertItem[
"Items"
].Controls[0];
TextBox txt2 = (TextBox)insertItem[
"Rate"
].Controls[0];
dtValues = (DataTable)Session[
"Table"
];
DataRow drValues = dtValues.NewRow();
drValues[
"Items"
] = txt1.Text;
drValues[
"Rate"
] = txt2.Text;
dtValues.Rows.Add(drValues);
//adding new row into datatable
dtValues.AcceptChanges();
Session[
"Table"
] = dtValues;
RadGrid1.Rebind();
}
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
//creating datatable
dtValues =
new
DataTable();
dtValues.Columns.Add(
"Items"
);
dtValues.Columns.Add(
"Rate"
);
if
(Session[
"Table"
] !=
null
)
{
dtValues = (DataTable)Session[
"Table"
];
}
RadGrid1.DataSource = dtValues;
//populate RadGrid with datatable
Session[
"Table"
] = dtValues;
}
protected
void
RadGrid1_UpdateCommand(
object
sender, GridCommandEventArgs e)
{
GridEditFormItem updateItem = (GridEditFormItem)e.Item;
int
rowindex=updateItem.ItemIndex;
TextBox txt1 = (TextBox)updateItem[
"Items"
].Controls[0];
TextBox txt2 = (TextBox)updateItem[
"Rate"
].Controls[0];
dtValues = (DataTable)Session[
"Table"
];
dtValues.Rows[rowindex][
"Items"
] = txt1.Text;
dtValues.Rows[rowindex][
"Rate"
] = txt2.Text;
dtValues.AcceptChanges();
//updating datatable with new values
Session[
"Table"
] = dtValues;
RadGrid1.Rebind();
}
Thanks,
Princy.
0

SUNIL
Top achievements
Rank 2
answered on 29 Dec 2010, 07:31 AM
Hi Princy,
Thanks for the response. The limitation in my case is that I cannot use need datasource event and the complete grid is created in code-behind. There is nothing in html file about the radgrid. I think because of this limitation there is something that I am missing. The example you gave works perfectly but it does not help me see what I am missing in my case.
Thanks
Sunil
Thanks for the response. The limitation in my case is that I cannot use need datasource event and the complete grid is created in code-behind. There is nothing in html file about the radgrid. I think because of this limitation there is something that I am missing. The example you gave works perfectly but it does not help me see what I am missing in my case.
Thanks
Sunil
0

Margo Noreen
Top achievements
Rank 1
Veteran
answered on 29 Dec 2010, 03:47 PM
I have been testing some code similar to this... I need a grid that is initially bound to a DataTable then lets the user insert and edit rows "at will"... Only after they click a separate "Save" button do I commit those changes back to the db. The changes are saved to the DataTable and the DataTable is stashed in and retrieved from ViewState (since we have users popping up multiple browser windows, and in that means Session state really doesn't work in that case!).
The test code I have works, but there's a lot of code! In particular, there's a lot of work around inserting rows and keeping track of that with a unique value for the DataKey value...
Any chance Telerik can create a version of the grid that supports this use case and hides the sheer amount of hand coding that's going on?
I need to use this on many, many pages in our web-based application and it concerns me to have so much hand coding going on just to be able to handle an "in memory" binding (or "manual binding" or ... whatever the best way to describe it is). I would think this is a very common use case. I don't want to bind to the database itself until AFTER all the inserts and edits are finalized by the user, then and only then commit the changes.
The test code I have works, but there's a lot of code! In particular, there's a lot of work around inserting rows and keeping track of that with a unique value for the DataKey value...
Any chance Telerik can create a version of the grid that supports this use case and hides the sheer amount of hand coding that's going on?
I need to use this on many, many pages in our web-based application and it concerns me to have so much hand coding going on just to be able to handle an "in memory" binding (or "manual binding" or ... whatever the best way to describe it is). I would think this is a very common use case. I don't want to bind to the database itself until AFTER all the inserts and edits are finalized by the user, then and only then commit the changes.
0

SUNIL
Top achievements
Rank 2
answered on 30 Dec 2010, 04:06 PM
The missing part in my case was that I was not saving the data source in viewstate or session, between postbacks. Since I was using an in-memory data source, I had to save it somewhere so when the page posted back, I could get the data for rebinding the grid.
Thanks
Sunil
Thanks
Sunil
0

SUNIL
Top achievements
Rank 2
answered on 30 Dec 2010, 08:27 PM
Margo,
I agree with you. I hope automatic inserts, updates and deletes worked for an in-memory data-table so there was no need to write so much code.
I had to write quite a bit of code for doing this.
Thanks
Sunil
I agree with you. I hope automatic inserts, updates and deletes worked for an in-memory data-table so there was no need to write so much code.
I had to write quite a bit of code for doing this.
Thanks
Sunil
0

Vasssek
Top achievements
Rank 1
answered on 27 Jan 2011, 11:57 AM
Hello,
for those who want to marshal multiple rows of data from a client application to SQL Server without multiple round trips, here is the link where you can find how to send datatable via sql parameter into stored procedure.
http://msdn.microsoft.com/en-us/library/bb675163(v=VS.100).aspx
One disadvantage is that this could be used only in SQL server 2008 version...
Have a nice day.
Vasssek
for those who want to marshal multiple rows of data from a client application to SQL Server without multiple round trips, here is the link where you can find how to send datatable via sql parameter into stored procedure.
http://msdn.microsoft.com/en-us/library/bb675163(v=VS.100).aspx
One disadvantage is that this could be used only in SQL server 2008 version...
Have a nice day.
Vasssek