Hi,
Am binding the RadGrid from dynamic data table in codebehind. After insert or updates happen the grid is not binding.
Below the code for reference.
ASPX
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
</script>
</telerik:RadCodeBlock>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
<div>
<telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" DecorationZoneID="demo" DecoratedControls="All"
EnableRoundedCorners="false" />
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" GridLines="None" AllowPaging="true" PageSize="20"
AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="true" AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true"
OnItemDeleted="RadGrid1_ItemDeleted" OnItemUpdated="RadGrid1_ItemUpdated" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemCommand="RadGrid1_ItemCommand" OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender">
<MasterTableView CommandItemDisplay="Top" DataKeyNames="EmpID">
<Columns>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn HeaderText="Employee ID" DataField="EmpID"></telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Employee Name" DataField="EmpName"></telerik:GridBoundColumn>
<telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings EditFormType="Template" >
<FormTemplate>
<table cellspacing="2" cellpadding="1" width="100%" border="0" rules="none" style="border-collapse: collapse;">
<tr>
<td style="padding-left: 5px; width: 50%;" align="left">
<asp:Label ID="Label3" runat="server" CssClass="DisplayLabel" Text="Employee ID"></asp:Label> <br />
<asp:TextBox ID="TextBox1" runat="server" Width="90%" Text='<%# Bind("EmpID") %>'></asp:TextBox>
</td>
<td style="padding-left: 5px; width: 50%;" align="left">
<asp:Label ID="Label1" runat="server" CssClass="DisplayLabel" Text="Employee Name"></asp:Label> <br />
<asp:TextBox ID="TextBox2" runat="server" Width="90%" Text='<%# Bind("EmpName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' CssClass="button"></asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" CssClass="button"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
</ClientSettings>
</telerik:RadGrid>
CS
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 TelerikGridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpID");
dt.Columns.Add("EmpName");
DataRow dr;
dr = dt.NewRow();
dr["EmpID"] = "1";
dr["EmpName"] = "Akash";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EmpID"] = "2";
dr["EmpName"] = "Babu";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EmpID"] = "3";
dr["EmpName"] = "Charles";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EmpID"] = "4";
dr["EmpName"] = "Dhulkar";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["EmpID"] = "5";
dr["EmpName"] = "Elizabeth";
dt.Rows.Add(dr);
Session["EmployeeDT"] = dt;
}
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
DataTable dtEmployee = (DataTable)Session["EmployeeDT"];
GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
GridButtonColumn deleteColumn = (GridButtonColumn)RadGrid1.MasterTableView.GetColumn("DeleteColumn");
if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
{
editColumn.Visible = false;
deleteColumn.Visible = false;
}
else if (e.CommandName == RadGrid.PerformInsertCommandName)
{
editColumn.Visible = false;
deleteColumn.Visible = false;
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtEmployeeID = editedItem.FindControl("TextBox1") as TextBox;
TextBox TXTEmployeeName = editedItem.FindControl("TextBox2") as TextBox;
DataRow dr;
dr = dtEmployee.NewRow();
dr["EmpID"] = txtEmployeeID.Text;
dr["EmpName"] = TXTEmployeeName.Text;
dtEmployee.Rows.Add(dr);
dtEmployee.AcceptChanges();
Session["dtLOB"] = dtEmployee;
}
else if (e.CommandName == RadGrid.EditCommandName)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtEmployeeID = editedItem.FindControl("TextBox1") as TextBox;
txtEmployeeID.Enabled = false;
}
else if (e.CommandName == RadGrid.UpdateCommandName)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtEmployeeID = editedItem.FindControl("TextBox1") as TextBox;
TextBox TXTEmployeeName = editedItem.FindControl("TextBox2") as TextBox;
foreach (GridDataItem item in RadGrid1.EditItems)
{
if (item.GetDataKeyValue("EmpID").ToString() == txtEmployeeID.Text)
{
for (int i = 0; i < dtEmployee.Rows.Count; i++)
{
if (dtEmployee.Rows[i]["EmpID"].ToString() == txtEmployeeID.Text)
{
dtEmployee.Rows[i]["EmpName"] = TXTEmployeeName.Text;
}
}
}
}
dtEmployee.AcceptChanges();
Session["dtLOB"] = dtEmployee;
}
else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
{
e.Canceled = true;
}
else
{
if (!editColumn.Visible)
editColumn.Visible = true;
}
}
protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
{
if (e.Exception != null)
{
e.KeepInEditMode = true;
e.ExceptionHandled = true;
}
}
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
}
protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
{
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//RadGrid1.EditIndexes.Add(0);
//RadGrid1.Rebind();
}
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["EmployeeDT"];
(sender as RadGrid).DataSource = dt;
}
}
Kindly help is there anything I missed out.
Regards,
Ganeshkumar