We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

I have question ; How to create editable grid with MySQL

I try to makeeditable grid with MySQL referring tohttp://asp.syncfusion.com/demos/web/grid/sqlbinding.aspx
But the grid does't move on browser.
please tell me How can i makeeditable grid with MySQL?
This is my source chord.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridtest.aspx.cs" Inherits="SyncfusionASPNETApplication1.gridtest" %>
 


OnServerAddRow="EditEvents_ServerAddRow" OnServerDeleteRow="EditEvents_ServerDeleteRow">
RecordDoubleClick="doubleClick" />

and cs file
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();
}
}
}

1 Reply

RU Ragavee U S Syncfusion Team July 4, 2016 12:05 PM UTC

Hi kyousuke, 

Thanks for your interest in Syncfusion products. 

We are sorry but we are unclear on what you actually mean by “But the grid does't move on browser.”. So could you please elaborate this query? 

We suspect that your requirement is to perform CRUD operations in grid. In order to make the grid editable, we need to enable the AllowEditing property of the Grid EditSettings. Please refer to the below help document for more information. 



We have prepared a sample using SQL data binding, which can be downloaded from the below location. 


Also, if you have mentioned like the server events were not triggered when editing/adding and saving the record, then we suggest you to refer the below knowledge base link. 


Regards, 
Ragavee U S. 


Loader.
Live Chat Icon For mobile
Up arrow icon