using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Syncfusion.JavaScript.Web;
using MySql.Data.MySqlClient;
namespace SyncfusionASPNETApplication1
{
public partial class gridtest : System.Web.UI.Page
{
DataTable dt = new DataTable("sample");
protected void Page_Load(object sender, EventArgs e)
{
var connectionString =
"Server=localhost;" +
"Database=exchange;" +
"User ID=root;" +
"Password=root";
if (!IsPostBack)
{
var myConnection = new MySqlConnection(connectionString);
dt = new DataTable("sample");
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "select * from sample";
cmd.CommandType = CommandType.Text;
MySqlDataAdapter da = new MySqlDataAdapter();
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["SQLConnectionString"].ToString());
dt = Session["SqlDataSource"] as DataTable;
Dictionary KeyVal = record as Dictionary;
if (eventType == "endEdit")
{
var Order = KeyVal.Values.ToArray();
foreach (DataRow dr in dt.Rows)
{
if (Convert.ToInt32(dr["date"]) == Convert.ToInt32(Order[0]))
{
dr["volume"] = Order[1];
dr.AcceptChanges();
}
}
}
else if (eventType == "endAdd")
{
var Order = KeyVal.Values.ToArray();
DataRow dr = dt.NewRow();
dr["date"] = Order[0];
dr["volume"] = Order[1];
dt.Rows.Add(dr);
}
else if (eventType == "endDelete")
{
var Order = KeyVal.Values.ToArray();
if (Session["SqlDataSource"] != null)
{
DataRow[] rows = dt.Select("date = " + Order[0]);
foreach (DataRow row in rows)
dt.Rows.Remove(row);
}
}
Session["SqlDataSource"] = dt;
dataBind();
}
}
}