Click here to Skip to main content
15,891,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a server client application having one winform which contains datagridview(dgv) with first column is checkbox column and last one is button column.
My scenario is when i click on checkbox column of row in dgv its row color changes to aqua and and when i click button of same row the row color changes to red and related data send to client this all happends in "datagidviewcellclickevent". Then after few seconds client send me "WITHOUTPROCESS" string and when i receive this string i set a "flagwithoutprocess" to true and check in timer that this flag is true or not (timer interval 1 sec) then i find its true so i changed the row color which is red currently after button press is converted into white like all rows back color.
My issue is when i again press the same row checkbox its color needs to be change in aqua color but this not happend all other row colors changes but the row i processed earlier is not changing its color to aqua . I cant trace where the mistake is , you can review my my code and if any mistake you found please let me know.

thnaks

What I have tried:

C#
timer to change status and color of row

        public void _timer_Tick(object sender, EventArgs e)
        {
             if (flagwithoutprocess == true)
                {
                    Appendlblmflash("Indent Discarded , send another indent");
                 
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (row.Index == indexxbox)
                        {
                            for (int i = 0; i <= row.Cells.Count - 1; i++)
                            {
                                row.Cells[i].Style.BackColor = Color.White;
                            }
                        }
                    }
                    flagwithoutprocess = false;
                }
                else { }
      }

Getdata method where server receive trigger from client to change row color

public void getdate()
{
   {
      string res = System.Text.Encoding.ASCII.GetString(temp);

      case "W":
             if (res.StartsWith("WITHOUTPROCESS"))
             {
                  flagwithoutprocess = true;
             }
              break;
    }
}

event where change the row color

private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
{
    try
    {
        var senderGrid = (DataGridView)sender;

       ///summary///
       ///for checkbox column///
                
      if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn && e.RowIndex >= 0)
      {
             indexxbox = e.RowIndex;

             foreach (DataGridViewRow row in dataGridView1.Rows)
             {
                 if (row.DefaultCellStyle.BackColor == Color.Aqua)
                 {
                      row.DefaultCellStyle.BackColor = Color.White;
                 }

                 if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
                 {
                  //Below code for getting checked value of checkbox
                   bool isChecked = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue;

                           // Checking value of checkbox
                           if (isChecked == true)
                           {
                               data = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                               fm = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                               to = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                              
                               //changing color of selected row
                               var r = this.dataGridView1.Rows[e.RowIndex];
                               r.DefaultCellStyle.BackColor = Color.Aqua;
                           }
                           row.Cells[SerialNo.Name].Value = false;

                           ifile.IniWriteValue("Config Section", "INDENT_NO", data);
                           txtindentcode.Text = data;
                       }
                      
                   }
                   
                }
               if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
                {
                    string msg = "Alert";
                    string cap = "Do you want to send indent to device?";
                    DialogResult dlgg = MessageBox.Show(cap, msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (dlgg == DialogResult.Yes)
                    {
                        if (mode == 0)
                        {
                            if (lblmflash.Text != "Device processing... please don't send indent")
                            {
                                indexxbutton = e.RowIndex;

                                if (indexxbutton == indexxbox)
                                {
                                    if (flagwithoutprocess == true)
                                    {
                                        foreach (DataGridViewRow row in dataGridView1.Rows)
                                        {
                                            if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
                                            {
                                                for (int i = 0; i <= row.Cells.Count - 1; i++)
                                                {
                                                    row.Cells[i].Style.BackColor = Color.White;
                                                }
                                            }
                                            row.Cells[SerialNo.Name].Value = false;
                                        }
                                        flagwithoutprocess = false;
                                    }

                                    else
                                    {
                                        if (networkStream != null)
                                        {
                                            isButtonClicked = true;

                                            string indata = ifile.IniReadValue("Config Section", "INDENT_NO");

                                            if (isButtonClicked == true)
                                            {
                                                string query = " SELECT item_code ,required_quantity " +
                                                               " FROM vw_DSInfo_indent_details " +
                                                               " WHERE indent_no = @ind " +
                                                               " AND indent_status like 'Approved' " +
                                                               " AND pending_qty not like '0%' ;";

                                                cmd = new MySqlCommand();
                                                cmd.CommandText = query;
                                                cmd.CommandType = CommandType.Text;
                                                cmd.Parameters.Add(new MySqlParameter("ind", indata));
                                                cmd.Connection = con;
                                                table = new DataTable();
                                                adapter = new MySqlDataAdapter(cmd);
                                                adapter.Fill(table);

                                                if (table.Rows.Count > 0 )
                                                {
                                                    List<string> code = new List<string>();
                                                    int h = 1;

                                                    foreach (DataRow row in table.Rows)
                                                    {
                                                        string itemcode = row["item_code"].ToString();
                                                        string[] reqqty = row["required_quantity"].ToString().Split('.');

                                                        code.Add(itemcode + "," + reqqty[0] + ",&*");

                                                        if (h == 200 || h == 400 || h == 600 || h == 800 || h == 1000 || h == 1200 || h == 1400 )
                                                        {
                                                            code.Add("`");
                                                        }
                                                        h++;
                                                    }               
                                                    
                                                    string onestr = string.Join("", code);
                                                    byte[] garbage = Encoding.ASCII.GetBytes("aaaaa");
                                                    networkStream.Write(garbage, 0, garbage.Length);

                                                    Thread.Sleep(500);

                                                    byte[] request = Encoding.ASCII.GetBytes("INDENT_REQ");
                                                    networkStream.Write(request, 0, request.Length);

                                                    byte[] fulldata = Encoding.ASCII.GetBytes(onestr);
                                                    networkStream.Write(fulldata, 0, fulldata.Length);

                                                    hash = Encoding.ASCII.GetBytes("#");
                                                    networkStream.Write(hash, 0, hash.Length);

                                                    ifile.IniWriteValue("Config Section", "INDENT_NO", "");

                                                    Appendlblmflash("");

                                                    foreach (DataGridViewRow row in dataGridView1.Rows)
                                                    {
                                                        if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
                                                        {
                                                            for (int i = 0; i <= row.Cells.Count - 1; i++)
                                                            {
                                                                row.Cells[i].Style.BackColor = Color.LightSalmon;
                                                            }
                                                        }
                                                        row.Cells[SerialNo.Name].Value = false;
                                                    }

                                                }

                                                else
                                                {
                                                    unscr = Encoding.ASCII.GetBytes("%");
                                                    networkStream.Write(unscr, 0, unscr.Length);
                                                    hash = Encoding.ASCII.GetBytes("#");
                                                    networkStream.Write(hash, 0, hash.Length);
                                                    string mmsg = "Alert";
                                                    string caption = "No items present for this Indent";
                                                    MessageBox.Show(caption, mmsg, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                                                    foreach (DataGridViewRow row in dataGridView1.Rows)
                                                    {
                                                        if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
                                                        {
                                                            for (int i = 0; i <= row.Cells.Count - 1; i++)
                                                            {
                                                                row.Cells[i].Style.BackColor = Color.LightSalmon;
                                                            }
                                                        }

                                                        row.Cells[SerialNo.Name].Value = false;
                                                    }

                                                    ifile.IniWriteValue("Config Section", "INDENT_NO", "");
                                                }
                                            }
                                            else // isbuttonclick else
                                            { }
                                        }

                                        else // network stream else
                                        {
                                            string mess = "Connection Status";
                                            string caption = "Device not connected! Connect Device properly.";
                                            MessageBox.Show(caption, mess, MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                                        }
                                    }
                                }
                                else  // indexx not match else
                                {
                                    //clicked cha ahe jithe sangach ahe wrong button clicked ahe mnun
                                    string mess = "Process Alert";
                                    string caption = "Wrong Button Pressed! Please Press selected row Button";
                                    MessageBox.Show(caption, mess, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                                }
                                // indexxbox = indexxbutton = 0;
                            }
                            else
                            {
                                string mess = "Process Alert";
                                string caption = "Device is processing, send indent after some time.";
                                MessageBox.Show(caption, mess, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                            }
                        }
                        else /// else part of indent mode and direct mode
                        {
                            string mess = "Process Alert";
                            string caption = "Device in Direct Transfer mode, Send indent when in Indent Transfer Mode";
                            MessageBox.Show(caption, mess, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                        }
                    }
                }
            }
            catch 
            {}
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900