Hi,
I create a small project with sfDataGrid, code bellow
If I use this strategy to update the database with the NetFramework DataGridView component or with DevExpress GridControl, all work fine, but with sfDataGrid control, only when I change the first row in the grid, the database is updated, all other rows don't update the database, for example, if the row with id=2 is the second row, the changes are not updated in the database, but after sort, the row with id=2 is the first row, then the changes are updated to the database.
I really need help, please.
using MySql.Data.MySqlClient;
using Syncfusion.Windows.Forms;
using System;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApp7
{
public partial class Form1 : MetroForm
{
private const string ConnectionString = "server=127.0.0.1;port=3306;database=lictestetopg;uid=root2;password=root2;";
private MySqlDataAdapter da = null;
private MySqlCommandBuilder cmdb = null;
private DataTable dt = null;
private BindingSource bs = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string query = $"SELECT email, password, name, age, status FROM users";
da = new MySqlDataAdapter(query, ConnectionString);
cmdb = new MySqlCommandBuilder(da);
dt = new DataTable();
bs = new BindingSource();
da.Fill(dt);
bs.DataSource = dt;
sfDataGrid1.DataSource = bs;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "FAC", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateDataToDatabase()
{
try
{
bs.SuspendBinding();
int rowsUp = da.Update(dt);
if (rowsUp == 0)
{
dt.RejectChanges();
}
else
{
dt.AcceptChanges();
}
}
catch (System.Data.DBConcurrencyException concEx)
{
dt.RejectChanges();
MessageBox.Show(concEx.Message, "FAC", MessageBoxButtons.OK, MessageBoxIcon.Error);
sfDataGrid1.AllowEditing = false;
}
catch (MySqlException sqlEx)
{
dt.RejectChanges();
MessageBox.Show(sqlEx.Message, "FAC", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.Exception ex)
{
dt.RejectChanges();
MessageBox.Show(ex.Message, "FAC", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
bs.ResumeBinding();
}
}
private void sfDataGrid1_RowValidated(object sender, Syncfusion.WinForms.DataGrid.Events.RowValidatedEventArgs e)
{
UpdateDataToDatabase();
}
}
}