private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// if(Control.ModifierKeys == Keys.Tab) {
//--Here we need to add datato the database...
try
{
//MessageBox.Show("end edit");
//--This section helps in validation..
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
var regex = new Regex(@"^[a-zA-Z0-9_]+$");
string name = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
// MessageBox.Show("name= " + name);
if (regex.IsMatch(name))
{
int length = name.Length;
if (length > 30)
{
//}
//else
//{
MessageBox.Show("You can only have name upto 30 characters");
dataGridView1.Refresh();
if (beginEditText == "")
{
dataGridView1.CurrentCell.Value = "";
}
else
{
dataGridView1.CurrentCell.Value = beginEditText;
}
return;
// }
}
//--Testing ..
// MessageBox.Show(" name = " + name);
//--we need to check that the table name enter doesnot matches previous values
if (name != beginEditText)
{
for (int i = 0; i < chartDetailList.Count; i++)
{
if (name == chartDetailList[i].chartName)
{
MessageBox.Show("chart can not have same name");
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = beginEditText;
return;
}
}
//--The value is ok
//--Now lets do the insert opeation for the data...
AddOrUpdateNewRecord(name, e);
// MessageBox.Show("regx name");
//--now lets add new rows..
dataGridView1.Refresh();
//dataGridView1.Rows.Clear();//Remove the rows
// BeginInvoke(new MethodInvoker(PopulateControl));
dataGridView1.Rows.Clear();
//--Lets get the data and refill the data in datagridview.
// PullDataFromDB();
//LETS FILL THE DATA
// fillDataGridView();
DataGridView_Show_Data();//This will do both pulling data and filing the data...
dataGridView1.Rows.Add();
//===============This one for refreshing the chart and selecting new created chart===//
//if (chartDetailList.Count > 0)
//{
if (dataGridView1.Rows.Count > 0) {
//We need to select the particular index
//--I hope this will do the replotting thing as well
if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
{
dataGridView1.Rows[e.RowIndex].Cells[1].Selected = true;//The row is selected
}
}
if (dataGridView1.Rows.Count > 0) //If there is data in datagridview then only
{
//MessageBox.Show("Clicked here row index: " + e.RowIndex);
var eventArgs = new DataGridViewCellEventArgs(1, e.RowIndex);
// or setting the selected cells manually before executing the function
dataGridView1.Rows[e.RowIndex].Cells[1].Selected = true;
dataGridView1_CellClick(sender, eventArgs);
// }
}
//===================end of selection of new created chart =======================//
}
else
{
// MessageBox.Show("No change in name");
}
///close of if we need to add here ...
}
else
{
MessageBox.Show("You can only have letters, numbers and underscores");
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = string.Empty;
}
}
//Catch exception before do this :
// this.dataGridView1.CurrentCell.ReadOnly = true;
}
catch (Exception ex)
{
MessageBox.Show("Edit and then press tab to commit changes " + ex.Message);
// var cell = dataGridView1.CurrentCell;
//if ( cell == null)
//{
// // dataGridView1.CurrentCell.Value = string.Empty;
// // return;
//}
//else {
// dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = beginEditText;
dataGridView1.CurrentCell.Value = beginEditText;
// }
}
}