WFA_psychometric_chart.Form1_main.dataGridView1_CellClick C# (CSharp) Method

dataGridView1_CellClick() public method

public dataGridView1_CellClick ( object sender, System.Windows.Forms.DataGridViewCellEventArgs e ) : void
sender object
e System.Windows.Forms.DataGridViewCellEventArgs
return void
        public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if(dataGridView1.Rows.Count <= 0)
            {
                return;
            }

            if (flagForTimer == 1)
            {
                if (atimer.Enabled) // Check if the timer is running
                {
                    //Return error.........
                    atimer.Enabled = false;
                    atimer.Dispose();
                    flagForTimer = 0;

                }
            }//close of flagfortimer

                      
            //--Lets check if the datagridview1 is empty or chartDetailList is empty
            if (chartDetailList.Count <= 0)
            {
                //--This flag is for OFFLINE mode Or ONLINE mode
                //--Now you are going to turn OFF the offline mode and go into realtime mode
                FlagForCntdBG_Update = 0;//currently in realtime mode so 1
               

                //--also make the radio button to be OFF and other to be ON
               // rb_ON.Checked = true;

                //--IFno chart is present then make edit mode on
                FlagForLockUnlock = 1;//   This means edit mode ON 
                LockAndUnlock();//This method will make things lock

                return;
            }

                      
            //--Showing the data on cell selected...
            // MessageBox.Show("CELL SELECT " );
            //When dgv is click it clicks twice this if is written to stop those twice click.

            if (flagSinglCellClick == 1)
            {
                flagSinglCellClick = 0;
                return;
            }
            if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
            {

                if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
                {
                    // MessageBox.Show("CELL SELECT ROW= " + e.RowIndex+",columns = "+e.ColumnIndex);
                    //The row is selected ..
                    for (int i = 0; i < chartDetailList.Count; i++)
                    {
                        //--Checking for evey item
                        if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == chartDetailList[i].chartName)
                        {
                            //if match found load
                            //data_load();
                            lb_title_display.Text = "";//--Historical plot needs to be cleared
                            indexRun = 0;//For inserting nodeName and lineName like node1,line1 etc
                            chart1.Enabled = true;

                            //--This flag is for OFFLINE mode Or ONLINE mode
                            //--Now you are going to turn OFF the offline mode and go into realtime mode
                            FlagForCntdBG_Update = 1;//currently in realtime mode so 1
                            
                            //--also make the radio button to be OFF and other to be ON
                            // rb_OFF.Checked = true;

                            FlagForLockUnlock = 0;//   This means edit mode OFF 
                            LockAndUnlock();//This method will make things lock

                            //This one(the signal) is for stoping the background running process instantly 
                            FlagForStopingRunningProcessInstantly = 1;


                            RefreshGraph();
                            /*
                            We need to identify which item in the list was clicked .
                              //Before we load line we need to identify the id of the chart to load the data..
                              */
                            //chartSelectedIDValue = chartDetailList[i].chartID;//--tHIS CONTAINS THE ID OF THE CHART
                            indexForWhichChartIsSelected = i;
                            //This has been changed
                            indexOfChartSelected = e.RowIndex;    //This value is changed 
                            LoadNodeAndLineFromDB(indexOfChartSelected);   //Lets make it passing the stirngs 

                            flagForInsertOrUpdateDataToDB = 1;
                            //--This is also completed..
                            ReDrawingLineAndNode();  //Done checking bbk modif: changing to alex db 


                            flagSinglCellClick = 1;

                            //lets enable the button
                       //  btn_pull_data.Enabled = true;
                        // button1.Enabled = true;
                            //--Now lets load the vaiables in dgv2
                            // loadVariableInDGV2();

                            //==============This part is for comfort zone=====================
                            //lets make the flag off first.
                            flagShow = 0;//off
                                         //this is for loading comfort zone 

                            //Lets clear all the chart first
                            default_comfort_zone_of_chart.Clear();
                            listchartComfortZoneInfoSingle.Clear();

                            LoadComfortZone(chartDetailList[indexForWhichChartIsSelected].chartID);

                            if (default_comfort_zone_of_chart.Count > 0)
                            {
                                //This means chart is present enable it
                                enableToolStripMenuItem.Enabled = true;

                                //AFTER WE know that there is chart then lets add this function  to check the show option
                                // MessageBox.Show("enabltool = ture"+ default_comfort_zone_of_chart.Count);
                                if (flagShow == 1)
                                {
                                    //  enableToolStripMenuItem.Enabled = true;
                                    enableToolStripMenuItem.Checked = true;
                                }
                                else
                                {
                                    // enableToolStripMenuItem.Enabled = false;
                                    enableToolStripMenuItem.Checked = false;
                                }


                            }
                            else
                            {
                                enableToolStripMenuItem.Enabled = false;
                                enableToolStripMenuItem.Checked = false;
                                //MessageBox.Show("enabltool = false" + default_comfort_zone_of_chart.Count);
                            }

                            //====================end of comfort zone=========================

                            //---Here we are going to call scan hardware function ----------------//

                            //StartHardwareScan();
                            //---End of call for scan hardware-----------------------------------//

                            //--Lets make the status of the device and web to be dissabled 
                            lb_web_status.Text = "inactive";
                            lb_device_status.Text = "disconnected";



                          
                            break;

                        }
                        else
                        {
                            flagForInsertOrUpdateDataToDB = 0;
                            RefreshGraph();
                        }
                    }






                }  //close of != null if
                else
                {
                    flagForInsertOrUpdateDataToDB = 0; //==insert or update OFF
                }


                //This is for cell formating and editing...
                //  if (dataGridView1.CurrentCell.ColumnIndex >= 0) { 
                try { 
                if (dataGridView1.CurrentCell.ColumnIndex == 1)
                {
                    // this.dataGridView1.BeginEdit(false);
                    this.dataGridView1.CurrentCell.ReadOnly = true;
                }
                }
                catch { }
               // }
                //===============This one is for realtime update and other things




                // if(atimer.Enabled == true)
                // {
                //   atimer.Dispose();
                //atimer.Stop();
                //}
                //===========================BackGroundWorker WORKS HERE============//

                //--First if backgound worker if working already we need to cancel and start the new instance

                //  backgroundWorker1.CancelAsync();//first cancel any work that is being done 
                //if(backgroundWorker1.CancellationPending== true)
                //{
                //   backgroundWorker1.can
                //}
                //if (lb_test1.InvokeRequired)
                //{
                //    lb_test1.Invoke(new Action(() => lb_test1.Text = "testActionInvokeFromOnClick"));
                //}
                //else
                //{
                //    lb_test1.Text = "testnoinvokeFromOnClick11";
                //}


                if (backgroundWorker1.IsBusy)
                {
                   // MessageBox.Show("background asyc dissabled");
                    backgroundWorker1.CancelAsync(); //==First dissabling background worker

                }

                //--This willcancel the pending
                //this is our worker
                //bgWorker = new BackgroundWorker();
                //// work happens in this method
                //bgWorker.DoWork += new DoWorkEventHandler(bg_DoWork);
                //bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);

                if (FlagForCntdBG_Update == 1)
                {
                    // MessageBox.Show("Entered");//--test
                    // chart1.Enabled = false;//should be dissabled 
                    //If the chart is in online mode then only do updating task

                    //--***********************Uncomment later********************//
                    InitTimerForDevice();
                    //--*************************end*****************************//

                }
                else
                {
                   // chart1.Enabled = true;
                }


                //===========================End of the backgound worker ====================================================//



                //================END of the real time update and other things .===========//



            }//close of columnindex if
            else if(e.RowIndex >= 0 && (e.ColumnIndex >= 0 && e.ColumnIndex <=1))
            {
                //we need to return and does not proceed forwared
                //select all 
               // dataGridView1.CurrentCell.Selected = true;

                return;
            }


        }
        //--Here is the task done by back ground worker

Usage Example

        private void button1_Click(object sender, EventArgs e)
        {
            //When ok button is clicked the there should be plotting of the comfort zone.
            //This has basically two function...

            /*
             * 1.insert the comfort zone info to database
             * 2. Plot the comfort zone based on the value passed.
             */
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
            {
                //chart id
                string chartid       = bs.chartDetailList[bs.indexOfChartSelected].chartID;
                string comfortzoneid = bs.listComfortZoneDetail[comboBox1.SelectedIndex].id;
                bs.InsertChartComfortzoneInfo(chartid, comfortzoneid);

                //now second part call the plot function
                // bs.PlotComfortZone()
                double mint = double.Parse(textBox1.Text);
                double maxt = double.Parse(textBox2.Text);
                double minh = double.Parse(textBox3.Text);
                double maxh = double.Parse(textBox4.Text);
                Color  c    = bs.listComfortZoneDetail[comboBox1.SelectedIndex].colorValue;
                string name = bs.listComfortZoneDetail[comboBox1.SelectedIndex].name;
                if (bs.default_comfort_zone_of_chart.Count > 0)
                {
                    if (bs.listchartComfortZoneInfoSingle.Count > 0)
                    {
                        //If default comfort zone is present then
                        //Clear previous one
                        //MessageBox.Show("Clear previous comfortzone");
                        bs.ClearComfortZone(double.Parse(bs.listchartComfortZoneInfoSingle[0].min_temp), double.Parse(bs.listchartComfortZoneInfoSingle[0].max_temp), double.Parse(bs.listchartComfortZoneInfoSingle[0].min_hum), double.Parse(bs.listchartComfortZoneInfoSingle[0].max_hum));
                    }
                }

                bs.PlotComfortZone(mint, maxt, minh, maxh, c, name);

                //insert of update database value
                bs.insertOrUpdateComfortChartSetting(chartid, comfortzoneid);

                if (bs.dataGridView1.Rows.Count > 0)  //If there is data then only do this one
                {
                    //set parameters of your event args
                    var eventArgs = new DataGridViewCellEventArgs(1, bs.dataGridView1.CurrentCell.RowIndex);
                    // or setting the selected cells manually before executing the function
                    bs.dataGridView1.Rows[bs.dataGridView1.CurrentCell.RowIndex].Cells[1].Selected = true;
                    bs.dataGridView1_CellClick(sender, eventArgs);
                }

                this.Close();
            }//close of if
        }
Form1_main