If you are able to update the table then you only need to include the WHERE clause while updating as you said its updating the whole table not a single row. Include a WHERE clause that might include an ID column that is primary key in database. i.e
Get the ProductIDName of row being edited and issue this statement, somewhat like:
string _sName = (eeditedItem["Name"].Controls[0] as TextBox).Text;
int _productId = Convert.ToInt32(rgSettings.MasterTableView.DataKeyValues[eeditedItem.ItemIndex]["ProductNameID"].ToString());
Query: "UPDATE [EOProductName] SET [Name] = _sName WHERE ProductNameID = _productId"
--
Hope that helps
Regards,
Syed Emad My Project ============
Please mark the post as answered if it was helpful
Error 1 The name 'rgSettings' does not exist in the current context C:\Documents and Settings\Lokeshb\Desktop\EyetecOptics\EyetecOptics\Productname.aspx.cs 39 42 C:\...\EyetecOptics\
this error will occur.........
Regards,
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
hey i have do this code like this.....and it work properly----but when item is edit the form is not closed and it still appearing and it is very confusing that record is updated till now or not.
so plz tell me how to close the form after edit...ma code is like this---\
Member
58 Points
213 Posts
how to update the data in RadGrid..
Apr 08, 2011 04:41 AM|tushartayalster|LINK
hello friend,
can u please tell me how to update the data in the radgrid control.
i have using the code below----
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Telerik.Web.UI;using System.Data;
public partial class Productname : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { SqlMaster sqlMaster = new SqlMaster(); string SelectQuery = " SELECT ProductNameID, Name FROM EOProductName"; DataTable dt = sqlMaster.ExecuteDataset(SelectQuery).Tables[0]; rgProductName.DataSource = dt; rgProductName.DataBind();
}
protected void rgProductName_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { if (e.CommandName == RadGrid.UpdateCommandName) { SqlMaster sqlMaster = new SqlMaster(); string UpdateQuery = "UPDATE EOProductName SET Name = Name;"; sqlMaster.ExecuteNonQuery(UpdateQuery); } }
protected void rgProductName_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e) { } protected void rgProductName_ItemInserted(object source, Telerik.Web.UI.GridInsertedEventArgs e) { }}
<telerik:RadGrid ID="rgProductName" runat="server" AutoGenerateColumns="false" PageSize="5" GridLines="None" AllowPaging="true" AllowSorting="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true" OnItemInserted="rgProductName_ItemInserted" OnItemUpdated="rgProductName_ItemUpdated" OnItemCommand="rgProductName_ItemCommand" Skin="Hay"> <MasterTableView DataKeyNames="ProductNameID" GridLines="None" InsertItemDisplay="Top" Width="98%"> <PagerStyle Mode="NextPrevAndNumeric" /> <Columns>
<telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Product Name"> <HeaderStyle Width="100px"></HeaderStyle> </telerik:GridBoundColumn>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn"> </telerik:GridEditCommandColumn>
</Columns>
</MasterTableView> </telerik:RadGrid>
plz plz plz tell me how to do this code
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
222 Points
72 Posts
Re: how to update the data in RadGrid..
Apr 08, 2011 06:15 AM|syedemad|LINK
Here is a sample code that might help you.
and the cs part
Try to use the NeedDataSource event of RadGrid, it can save you lot of extra work and next time past lengthy code though the code editor :)
Syed Emad
My Project
============
Please mark the post as answered if it was helpful
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 08, 2011 06:38 AM|tushartayalster|LINK
why you can use
edititem template and item template
it can create two update cancel button after clicking edit....
and it cannot update the data.
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 08, 2011 06:55 AM|tushartayalster|LINK
i dont know but it can create two update cancel button when clicking on the edit button......
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 08, 2011 07:26 AM|tushartayalster|LINK
hey i have do this code
protected void rgProductName_NeedDataSource(object source, GridNeedDataSourceEventArgs e) { SqlMaster sqlMaster = new SqlMaster(); string SelectQuery = " SELECT ProductNameID, Name FROM EOProductName"; DataTable dt = sqlMaster.ExecuteDataset(SelectQuery).Tables[0]; rgProductName.DataSource = dt; }
protected void rgProductName_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { GridEditableItem eeditedItem = e.Item as GridEditableItem; string Name = (eeditedItem["Name"].Controls[0] as TextBox).Text;
try { SqlMaster sqlMaster = new SqlMaster(); //Update Query to update the Datatable string updateQuery = "UPDATE EOProductName set Name='" + Name + "'"; sqlMaster.ExecuteNonQuery(updateQuery);
} catch (Exception ex) { rgProductName.Controls.Add(new LiteralControl("Unable to update Employee. Reason: " + ex.Message)); e.Canceled = true; }
} <div></div>
<telerik:RadGrid ID="rgProductName" runat="server" AutoGenerateColumns="false" PageSize="5" GridLines="None" AllowPaging="true" AllowSorting="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true" OnUpdateCommand="rgProductName_UpdateCommand" Skin="Hay" onneeddatasource="rgProductName_NeedDataSource"> <MasterTableView DataKeyNames="ProductNameID" GridLines="None" CommandItemDisplay ="Top" Width="98%"> <PagerStyle Mode="NextPrevAndNumeric" />
<Columns> <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Product Name"> <HeaderStyle Width="100px"></HeaderStyle> </telerik:GridBoundColumn>
<telerik:GridEditCommandColumn> </telerik:GridEditCommandColumn> </Columns> <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for ProductName with ID {0}" CaptionDataField="ProductNameID"> <FormTableItemStyle Wrap="False"></FormTableItemStyle> <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module" Height="110px" Width="100%" /> <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit"> </EditColumn> <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> </EditFormSettings> <ExpandCollapseColumn Visible="False"> <HeaderStyle Width="19px"></HeaderStyle> </ExpandCollapseColumn> <RowIndicatorColumn Visible="False"> <HeaderStyle Width="20px" /> </RowIndicatorColumn> </MasterTableView> </telerik:RadGrid>
it will update the entire row
but i want only the single row is updated...
plz plz plz tell me how to change the code.
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
222 Points
72 Posts
Re: how to update the data in RadGrid..
Apr 08, 2011 08:24 AM|syedemad|LINK
If you are able to update the table then you only need to include the WHERE clause while updating as you said its updating the whole table not a single row. Include a WHERE clause that might include an ID column that is primary key in database. i.e
Get the ProductIDName of row being edited and issue this statement, somewhat like:
string _sName = (eeditedItem["Name"].Controls[0] as TextBox).Text;
int _productId = Convert.ToInt32(rgSettings.MasterTableView.DataKeyValues[eeditedItem.ItemIndex]["ProductNameID"].ToString());
Query: "UPDATE [EOProductName] SET [Name] = _sName WHERE ProductNameID = _productId"
--
Hope that helps
Syed Emad
My Project
============
Please mark the post as answered if it was helpful
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 12, 2011 02:49 AM|tushartayalster|LINK
Error 1 The name 'rgSettings' does not exist in the current context C:\Documents and Settings\Lokeshb\Desktop\EyetecOptics\EyetecOptics\Productname.aspx.cs 39 42 C:\...\EyetecOptics\
this error will occur.........
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 12, 2011 03:01 AM|tushartayalster|LINK
hey i have do this code like this.....and it work properly----but when item is edit the form is not closed and it still appearing and it is very confusing that record is updated till now or not.
so plz tell me how to close the form after edit...ma code is like this---\
<telerik:RadGrid ID="rgProductName" runat="server" AutoGenerateColumns="false" PageSize="5" GridLines="None" AllowPaging="true" AllowSorting="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true" OnUpdateCommand="rgProductName_UpdateCommand" Skin="Hay" onneeddatasource="rgProductName_NeedDataSource"> <MasterTableView DataKeyNames="ProductNameID" GridLines="None" CommandItemDisplay ="Top" Width="98%"> <PagerStyle Mode="NextPrevAndNumeric" />
<Columns> <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Product Name"> <HeaderStyle Width="100px"></HeaderStyle> </telerik:GridBoundColumn>
<telerik:GridEditCommandColumn> </telerik:GridEditCommandColumn> </Columns> <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for ProductName with ID {0}" CaptionDataField="ProductNameID"> <FormTableItemStyle Wrap="False"></FormTableItemStyle> <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module" Height="110px" Width="100%" /> <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit"> </EditColumn> <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> </EditFormSettings> <ExpandCollapseColumn Visible="False"> <HeaderStyle Width="19px"></HeaderStyle> </ExpandCollapseColumn> <RowIndicatorColumn Visible="False"> <HeaderStyle Width="20px" /> </RowIndicatorColumn> </MasterTableView> </telerik:RadGrid> <div></div>
protected void rgProductName_NeedDataSource(object source, GridNeedDataSourceEventArgs e) { SqlMaster sqlMaster = new SqlMaster(); string SelectQuery = " SELECT ProductNameID, Name FROM EOProductName"; DataTable dt = sqlMaster.ExecuteDataset(SelectQuery).Tables[0]; rgProductName.DataSource = dt; }
protected void rgProductName_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { GridEditableItem eeditedItem = e.Item as GridEditableItem; string ID = eeditedItem.OwnerTableView.DataKeyValues[eeditedItem.ItemIndex]["ProductNameID"].ToString(); string Name = (eeditedItem["Name"].Controls[0] as TextBox).Text;
try { SqlMaster sqlMaster = new SqlMaster(); //Update Query to update the Datatable string updateQuery = "UPDATE EOProductName set Name='" + Name + "'where ProductNameID='" + ID + "'"; sqlMaster.ExecuteNonQuery(updateQuery);
} catch (Exception ex) { rgProductName.Controls.Add(new LiteralControl("Unable to update Employee. Reason: " + ex.Message)); e.Canceled = true; }
}
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 12, 2011 06:18 AM|tushartayalster|LINK
i will doit and now code is working absolutely fine----
<telerik:RadGrid ID="rgMaterial" runat="server" AutoGenerateColumns="false" PageSize="5" GridLines="None" AllowPaging="true" AllowSorting="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="false" OnUpdateCommand="rgMaterial_UpdateCommand" Skin="Hay" OnNeedDataSource="rgMaterial_NeedDataSource"> <MasterTableView DataKeyNames="MaterialID" GridLines="None" Width="98%"> <PagerStyle Mode="NextPrevAndNumeric" /> <Columns> <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Material Name"> <HeaderStyle Width="100px"></HeaderStyle> </telerik:GridBoundColumn> <telerik:GridEditCommandColumn> </telerik:GridEditCommandColumn> </Columns> <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for MaterialName with ID {0}" CaptionDataField="MaterialID"> <FormTableItemStyle Wrap="False"></FormTableItemStyle> <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module" Height="110px" Width="100%" /> <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit"> </EditColumn> <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> </EditFormSettings> <ExpandCollapseColumn Visible="False"> <HeaderStyle Width="19px"></HeaderStyle> </ExpandCollapseColumn> <RowIndicatorColumn Visible="False"> <HeaderStyle Width="20px" /> </RowIndicatorColumn> </MasterTableView> </telerik:RadGrid>
protected void rgMaterial_NeedDataSource(object source, GridNeedDataSourceEventArgs e) { SqlMaster sqlMaster = new SqlMaster(); string SelectQuery = " SELECT MaterialID, Name FROM EOMaterial"; DataTable dt = sqlMaster.ExecuteDataset(SelectQuery).Tables[0]; rgMaterial.DataSource = dt;
}
protected void rgMaterial_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) {
GridEditableItem eeditedItem = e.Item as GridEditableItem; string ID = eeditedItem.OwnerTableView.DataKeyValues[eeditedItem.ItemIndex]["MaterialID"].ToString(); string Name = (eeditedItem["Name"].Controls[0] as TextBox).Text;
try {
SqlMaster sqlMaster = new SqlMaster(); string updateQuery = "UPDATE EOMaterial set Name='" + Name + "'where MaterialID='" + ID + "'"; sqlMaster.ExecuteNonQuery(updateQuery);
} catch (Exception ex) { rgMaterial.Controls.Add(new LiteralControl("Unable to update Product Name. Reason: " + ex.Message)); e.Canceled = true; }
}
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 18, 2011 04:11 AM|tushartayalster|LINK
<telerik:RadGrid ID="rgMaterial" runat="server" AutoGenerateColumns="false" PageSize="5" GridLines="None" AllowPaging="true" AllowSorting="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="false" OnUpdateCommand="rgMaterial_UpdateCommand" Skin="Hay" OnNeedDataSource="rgMaterial_NeedDataSource"> <MasterTableView DataKeyNames="MaterialID" GridLines="None" Width="98%"> <PagerStyle Mode="NextPrevAndNumeric" /> <Columns> <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Material Name"> <HeaderStyle Width="100px"></HeaderStyle> </telerik:GridBoundColumn> <telerik:GridEditCommandColumn> </telerik:GridEditCommandColumn> </Columns> <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for MaterialName with ID {0}" CaptionDataField="MaterialID"> <FormTableItemStyle Wrap="False"></FormTableItemStyle> <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module" Height="110px" Width="100%" /> <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit"> </EditColumn> <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> </EditFormSettings> <ExpandCollapseColumn Visible="False"> <HeaderStyle Width="19px"></HeaderStyle> </ExpandCollapseColumn> <RowIndicatorColumn Visible="False"> <HeaderStyle Width="20px" /> </RowIndicatorColumn> </MasterTableView> </telerik:RadGrid> <div></div>
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.
Member
58 Points
213 Posts
Re: how to update the data in RadGrid..
Apr 18, 2011 04:12 AM|tushartayalster|LINK
Tushar Tayal
Remember to click “Mark as Answer” to the post that helps you.