<ej:Grid ID="EmployeesGrid2" runat="server" DataSourceID="SqlData" EnableLoadOnDemand="false" AllowTextWrap="True" AllowPaging="True">
<EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" />
<ToolbarSettings ToolbarItems="add,edit,delete,update,cancel" ShowToolbar="true"></ToolbarSettings>
<Columns>
<ej:Column Field="OrderID" IsPrimaryKey="true" IsIdentity="True"/>
<ej:Column Field="EmployeeID" />
<ej:Column Field="Freight" Format="{0:c2}" />
<ej:Column Field="ShipCity" />
</Columns>
</ej:Grid>
<asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"
InsertCommand="INSERT INTO [Orders] ([Freight], [EmployeeID], [ShipCity])
VALUES (@Freight, @EmployeeID, @ShipCity)"
DeleteCommand=
"DELETE FROM [Orders] WHERE [OrderID] = @OrderID"
UpdateCommand="UPDATE [Orders] SET
[Freight] = @Freight, [EmployeeID] = @EmployeeID, [ShipCity] = @ShipCity
WHERE [OrderID] = @OrderID">
<UpdateParameters>
<asp:Parameter Name="Freight" Type="Double" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
<asp:Parameter Name="ShipCity" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Freight" Type="Double" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
<asp:Parameter Name="ShipCity" Type="String" />
</InsertParameters>
<DeleteParameters>
<asp:Parameter Name="OrderID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
|
<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True" AllowSorting="True" OnServerEditRow="EditEvents_ServerEditRow"
OnServerAddRow="EditEvents_ServerAddRow" OnServerDeleteRow="EditEvents_ServerDeleteRow">
<Columns>
<ej:Column Field="OrderID" IsPrimaryKey="true" />
<ej:Column Field="ShipCity" />
</Columns>
<EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings>
<ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
</ej:Grid>
DataTable dt = new DataTable("Order");
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);
OrdersGrid.DataSource = dt;
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());
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["ShipCity"] = Order[3];
dr.AcceptChanges();
}
}
myConnection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "UPDATE Orders SET ShipCity = @ShipCity WHERE OrderID = @OrderID";
cmd.Parameters.AddWithValue("@OrderID", Order[0]);
cmd.Parameters.AddWithValue("@ShipCity", Order[1]);
cmd.ExecuteNonQuery();
myConnection.Close();
}
else if (eventType == "endAdd")
{
var Order = KeyVal.Values.ToArray();
DataRow dr = dt.NewRow();
dr["OrderID"] = Order[0];
dr["ShipCity"] = Order[1];
dt.Rows.Add(dr);
myConnection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "SET IDENTITY_INSERT Orders ON";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO Orders(OrderID,ShipCity) VALUES(@OrderID,@ShipCity)";
cmd.Parameters.AddWithValue("@ShipCity", Order[1]);
cmd.ExecuteNonQuery();
myConnection.Close();
}
else if (eventType == "endDelete")
{
var Order = KeyVal.Values.ToArray();
myConnection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "SET IDENTITY_INSERT Orders ON";
cmd.ExecuteNonQuery();
cmd.CommandText = "DELETE FROM Orders WHERE OrderID = @OrderID";
cmd.Parameters.AddWithValue("@OrderID", Order[0]);
cmd.ExecuteNonQuery();
myConnection.Close();
if (Session["SqlDataSource"] != null)
{
DataRow[] rows = dt.Select("OrderID = " + Order[0]);
foreach (DataRow row in rows)
dt.Rows.Remove(row);
}
}
OrdersGrid.DataSource = dt;
OrdersGrid.DataBind();
}
|
<ej:Grid ID="EmployeesGrid2" runat="server" DataSourceID="SqlData" EnableLoadOnDemand="false" AllowTextWrap="True" AllowPaging="True"> <EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" />
<ToolbarSettings ToolbarItems="add,edit,delete,update,cancel" ShowToolbar="true"></ToolbarSettings>
<Columns>
<ej:Column Field="OrderID" IsPrimaryKey="true" IsIdentity="True" />
<ej:Column Field="EmployeeID" />
<ej:Column Field="Freight" Format="{0:c2}" />
<ej:Column Field="ShipCity" />
</Columns>
</ej:Grid>
<asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
SelectCommand="SELECT * FROM [Orders] WHERE ([OrderID] = @OrderID)"
InsertCommand="INSERT INTO [Orders] ([Freight], [EmployeeID], [ShipCity])
VALUES (@Freight, @EmployeeID, @ShipCity)"
DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID"
UpdateCommand="UPDATE [Orders] SET
[Freight] = @Freight, [EmployeeID] = @EmployeeID, [ShipCity] = @ShipCity
WHERE [OrderID] = @OrderID">
…………………………………………………………..
<SelectParameters>
<asp:SessionParameter Name="OrderID" SessionField="OrderID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
// code behind protected void Page_Load(object sender, EventArgs e) {
this.Session["OrderID"] = "10250";
} |