Reputation: 15
I'm having a problem, so i have a form that contains an "insert and new" and an "insert and close" buttons . the insert and new button lets you insert a row and reopens the form to insert another row . but the insert and close button closes after adding a row . Anyways my problem is the following : I am using the command field in order to insert,update or delete from my database .
the insert and close button is working but the insert and new button is not because I'm not being able to add a row in my table . I just want to see if i can add an "Insert and new"button in my command field so i could use it (knowing that i already used all the buttons( delete,insert,update..) .
Thanks .
Upvotes: -1
Views: 784
Reputation: 11154
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView CommandItemDisplay="Top" EditMode="InPlace">
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<InsertItemTemplate>
<asp:Button ID="Button1" runat="server" Text="insert and new" CommandName="insertandnew" />
<asp:Button ID="Button2" runat="server" Text="insert and close" CommandName="insertandclose" />
</InsertItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"},
new { ID = 4, Name = "Name4"},
new { ID = 5, Name = "Name5"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "insertandnew")
{
// Perform Your Insert operation
e.Canceled = true;
//Set insertmonde once again
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid1.Rebind();
}
else if (e.CommandName == "insertandclose")
{
// Perform Your Insert operation
RadGrid1.MasterTableView.IsItemInserted = false;
RadGrid1.Rebind();
}
}
Upvotes: 0