<ej:Grid ID="FlatGrid" runat="server" AllowPaging="true">
<EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></EditSettings>
<ToolbarSettings ShowToolbar="true" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
<Columns>
<ej:Column Field="OrderID" IsPrimaryKey="true"/>
<ej:Column Field="CustomerID" />
<ej:Column Field="EmployeeID"/>
<ej:Column Field="ShipCity"/>
<ej:Column Field="ShipCountry"/>
</Columns>
</ej:Grid> |
<ej:Grid AllowSearching="true" AllowSorting="true" DataSourceCachingMode="ViewState" ID="Grid" AllowFiltering="true" AllowGrouping="true" AllowPaging="true" IsResponsive="true" runat="server" OnServerExcelExporting="Grid_ServerExcelExporting">
<EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></EditSettings>
<FilterSettings FilterType="Excel"></FilterSettings>
<ToolbarSettings ShowToolbar="true" ToolbarItems="excelExport,add,edit,delete,update,cancel"></ToolbarSettings>
<Columns>
<ej:Column Field="Boat_ID" Visible="true" IsPrimaryKey="true" />
<ej:Column Field="Name" HeaderText="Name" />
<ej:Column Field="Notes" HeaderText="Notes" />
<ej:Column Field="Estimated_Total_Hours" HeaderText="Estimated Hours" />
<ej:Column Field="project_ID" HeaderText="ProjectID" />
</Columns>
</ej:Grid>
<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True" AllowSorting="True" OnServerEditRow="EditEvents_ServerEditRow"
OnServerAddRow="EditEvents_ServerAddRow" OnServerDeleteRow="EditEvents_ServerDeleteRow">
<ClientSideEvents ActionComplete="complete" EndAdd="endAdd" EndDelete="endDelete" EndEdit="endEdit"
RecordDoubleClick="doubleClick" />
<Columns>
<ej:Column Field="OrderID" HeaderText=" Order ID" IsPrimaryKey="True" TextAlign="Right" Width="75">
</ej:Column>
. . .
</Columns>
<EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings>
<ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
</ej:Grid>
<asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
Serverside:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ToString());
dt = new DataTable("Order");
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "select * from Orders";
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
da.Fill(dt);
Session["SqlDataSource"] = dt;
dataBind();
}
}
protected void dataBind()
{
OrdersGrid.DataSource = (DataTable)Session["SqlDataSource"];
OrdersGrid.DataBind();
}
protected void EditEvents_ServerEditRow(object sender, GridEventArgs e)
{
EditAction(e.EventType, e.Arguments["data"]);
}
protected void EditEvents_ServerAddRow(object sender, GridEventArgs e)
{
EditAction(e.EventType, e.Arguments["data"]);
}
protected void EditEvents_ServerDeleteRow(object sender, GridEventArgs e)
{
EditAction(e.EventType, e.Arguments["data"]);
}
protected void EditAction(string eventType, object record)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ToString());
dt = Session["SqlDataSource"] as DataTable;
Dictionary<string, object> KeyVal = record as Dictionary<string, object>;
if (eventType == "endEdit")
{
var Order = KeyVal.Values.ToArray();
foreach (DataRow dr in dt.Rows)
{
if (Convert.ToInt32(dr["OrderID"]) == Convert.ToInt32(Order[0]))
{
dr["EmployeeID"] = Order[1];
dr["Freight"] = Order[2];
dr["ShipCity"] = Order[3];
dr["ShipCountry"] = Order[4];
dr.AcceptChanges();
}
}
}
else if (eventType == "endAdd")
{
var Order = KeyVal.Values.ToArray();
DataRow dr = dt.NewRow();
dr["OrderID"] = Order[0];
dr["EmployeeID"] = Order[1];
dr["Freight"] = Order[2];
dr["ShipCity"] = Order[3];
dr["ShipCountry"] = Order[4];
dt.Rows.Add(dr);
}
else if (eventType == "endDelete")
{
var Order = KeyVal.Values.ToArray();
if (Session["SqlDataSource"] != null)
{
DataRow[] rows = dt.Select("OrderID = " + Order[0]);
foreach (DataRow row in rows)
dt.Rows.Remove(row);
}
}
Session["SqlDataSource"] = dt;
dataBind();
} |