DataTable.GetChanges returns null sometimes even though changes have been made.
1.Edit a cell in first row (C,1), change is applied and saves successfully to DB. Cursor moves to cell (C,2) on return.
This is more odd than I thought as DataTable changeTable = DataTableTopLevel.GetChanges(); returns null, but it still saves
2.Edit a cell in second row (C,2), value changes back on return key. Also cursor does not move to (C,3) BUT moves to (A,3).
DataTable changeTable = DataTableTopLevel.GetChanges(); returns non null, but is not saved.
3.Edit a cell in third row (C,3), change is applied and saves successfully to DB. Cursor moves to cell (C,4) on return.
public partial class MeServerMainForm : Form
{
string connServer;
string qryServerTopLevel;
string qryServerBottomLevel;
SqlDataAdapter SqlAdapterTopLevel;
SqlDataAdapter SqlAdapterBottomLevel;
SqlCommandBuilder SqlCommandBuilderTopLevel;
SqlCommandBuilder SqlCommandBuilderBottomLevel;
DataTable DataTableTopLevel;
DataTable DataTableBottomLevel;
BindingSource BindingSourceTopLevel;
BindingSource BindingSourceBottomLevel;
bool TopLevelCellValueChanged;
public MeServerMainForm()
{
InitializeComponent();
InitData();
TopLevelCellValueChanged = false;
}
public void InitData()
{
connServer = "Data Source=192.168.0.11;Initial Catalog=Phil;User Id=Philip;Password=Philip;";
qryServerTopLevel = @"SELECT
MeID
,MeTypeID
,MeTitle
,CreatedDateTime
,ReadOnly
,ParentMeID
,Question
,Answer
,MeScore
,MeLastTestDate
FROM MyMees
WHERE ParentMeID = 0 ";
SqlAdapterTopLevel = new SqlDataAdapter(qryServerTopLevel, connServer);
SqlCommandBuilderTopLevel = new SqlCommandBuilder(SqlAdapterTopLevel);
DataTableTopLevel = new DataTable();
SqlAdapterTopLevel.Fill(DataTableTopLevel);
BindingSourceTopLevel = new BindingSource();
BindingSourceTopLevel.DataSource = DataTableTopLevel;
DataGridViewTopLevel.DataSource = BindingSourceTopLevel;
qryServerBottomLevel = @"SELECT
MeID
,MeTypeID
,MeTitle
,CreatedDateTime
,ReadOnly
,ParentMeID
,Question
,Answer
,MeScore
,MeLastTestDate
FROM MyMees
WHERE ParentMeID = 2 ";
SqlAdapterBottomLevel = new SqlDataAdapter(qryServerBottomLevel, connServer);
SqlCommandBuilderBottomLevel = new SqlCommandBuilder(SqlAdapterBottomLevel);
DataTableBottomLevel = new DataTable();
SqlAdapterBottomLevel.Fill(DataTableBottomLevel);
BindingSourceBottomLevel = new BindingSource();
BindingSourceBottomLevel.DataSource = DataTableBottomLevel;
DataGridViewBottomLevel.DataSource = BindingSourceBottomLevel;
DataTableTopLevel.AcceptChanges();
}
private void loadMeTreeToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void DataGridViewTopLevel_RowLeave(object sender, DataGridViewCellEventArgs e)
{
DataTable changeTable = DataTableTopLevel.GetChanges();
if (changeTable != null)
{
SqlAdapterTopLevel.Update(DataTableTopLevel);
DataTableTopLevel.AcceptChanges();
DataTableTopLevel.Clear();
SqlAdapterTopLevel.Fill(DataTableTopLevel);
TopLevelCellValueChanged = false;
}
}
Any ideas?
Thanks
Phil