WFA_psychometric_chart.Form_handler.dataGridView1_CellEndEdit_1 C# (CSharp) Method

dataGridView1_CellEndEdit_1() private method

private dataGridView1_CellEndEdit_1 ( object sender, System.Windows.Forms.DataGridViewCellEventArgs e ) : void
sender object
e System.Windows.Forms.DataGridViewCellEventArgs
return void
        private void dataGridView1_CellEndEdit_1(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(" cell end edit ,name = "+name);
                        //--we need to check that the table name enter doesnot matches previous values

                        if(name != beginEditText) {
                        for(int i= 0; i < listForDataFromDB.Count; i++)
                        {
                            if(name == listForDataFromDB[i].name)
                            {
                                MessageBox.Show("Handler 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();

                        dataGridView1.Rows.Add();

                        }
                        else
                        {
                            MessageBox.Show("No change in name");
                        }

                    }
                    else
                    {
                        MessageBox.Show("You can only have letters, numbers and underscores");
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = string.Empty;

                    }

                }

                 } catch(Exception ex){
                MessageBox.Show("Edit and then press tab to commit changes");
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = beginEditText;

            }
        }