Insert and Update in Telerik Radgrid
Insert and Update in Telerik Radgrid
Step 1:Database design
Table Creation.
CREATE TABLE [dbo].[GridView](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[salary] [decimal](18, 0) NULL,
[country] [varchar](50) NULL,
[city] [varchar](50) NULL,
CONSTRAINT [PK_GridView] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Step 2:Design web from .aspx page
<div>
<asp:ScriptManager ID="sx" runat="server"></asp:ScriptManager>
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
Width="800px" AllowSorting="true" AllowAutomaticUpdates="true"
AllowAutomaticInserts="true" OnItemInserted="RadGrid1_ItemInserted"
OnItemUpdated="RadGrid1_ItemUpdated" OnItemCreated="RadGrid1_ItemCreated">
<MasterTableView DataKeyNames="ID" CommandItemDisplay="Top" Width="100%">
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditColumn">
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" ReadOnly="true" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" FilterControlWidth="80%">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Age" HeaderText="Age" FilterControlWidth="80%">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Salary" HeaderText="Salary" FilterControlWidth="80%">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Country" HeaderText="Country" FilterControlWidth="80%">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="city" HeaderText="city" FilterControlWidth="80%">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet" ConnectionString="<%$ ConnectionStrings:kandy %>"
SelectCommand="SELECT [ID], [name], [age],
[salary],[country],[city] FROM [GridView]" UpdateCommand="UPDATE
[GridView] SET [name] = @name, [age] = @age, [salary] =
@salary,[country]=@country WHERE [ID] = @ID"
InsertCommand="INSERT INTO [GridView] ([name], [age],
[salary],[country]) VALUES (@name, @age, @salary,@country)">
<UpdateParameters>
<asp:Parameter Name="name" Type="String"></asp:Parameter>
<asp:Parameter Name="age" Type="Int32"></asp:Parameter>
<asp:Parameter Name="salary" Type="Int32"></asp:Parameter>
<asp:Parameter Name="ID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="country" Type="String"></asp:Parameter>
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="name" Type="String"></asp:Parameter>
<asp:Parameter Name="age" Type="Int32"></asp:Parameter>
<asp:Parameter Name="salary" Type="Int32"></asp:Parameter>
<asp:Parameter Name="country" Type="String"></asp:Parameter>
</InsertParameters>
</asp:SqlDataSource>
</div>
Step 3:Logic on .Cs page
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
Hashtable values = new Hashtable();
e.Item.ExtractValues(values);
}
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)
{
Hashtable values = new Hashtable();
e.Item.ExtractValues(values);
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditFormInsertItem)
{
GridEditFormInsertItem insertForm = e.Item as GridEditFormInsertItem;
GridTextBoxColumnEditor editorTitle = insertForm.EditManager.GetColumnEditor("name") as GridTextBoxColumnEditor;
RequiredFieldValidator requiredFieldValidator1 = new RequiredFieldValidator();
requiredFieldValidator1.ID = "RequiredFieldValidatorLastName";
requiredFieldValidator1.ControlToValidate =
editorTitle.TextBoxControl.ID;
requiredFieldValidator1.ErrorMessage
= " * This field is required";
editorTitle.TextBoxControl.Parent.Controls.Add(requiredFieldValidator1);
GridTextBoxColumnEditor age = insertForm.EditManager.GetColumnEditor("Age") as GridTextBoxColumnEditor;
RequiredFieldValidator age_v = new RequiredFieldValidator();
age_v.ID = "ff";
age_v.ControlToValidate =
age.TextBoxControl.ID;
age_v.ErrorMessage = " * This field is required";
age.TextBoxControl.Parent.Controls.Add(age_v);
}
else if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editForm = e.Item as GridEditableItem;
GridTextBoxColumnEditor editorTitle = editForm.EditManager.GetColumnEditor("name") as GridTextBoxColumnEditor;
RequiredFieldValidator requiredFieldValidator1 = new RequiredFieldValidator();
requiredFieldValidator1.ID = "RequiredFieldValidatorLastName";
requiredFieldValidator1.ControlToValidate =
editorTitle.TextBoxControl.ID;
requiredFieldValidator1.ErrorMessage = "
* This field is required";
editorTitle.TextBoxControl.Parent.Controls.Add(requiredFieldValidator1);
GridTextBoxColumnEditor age = editForm.EditManager.GetColumnEditor("Age") as GridTextBoxColumnEditor;
RequiredFieldValidator age_v = new RequiredFieldValidator();
age_v.ID = "ff";
age_v.ControlToValidate =
age.TextBoxControl.ID;
age_v.ErrorMessage = " * This field is required";
age.TextBoxControl.Parent.Controls.Add(age_v);
}
}
Comments
Post a Comment