I have datagridview(dgv) in windows form application I need to set its back color to white in one method and i did it ,I cheked in debugging the expected row color changed but not showing in dgv ,what could be the reason behind this?
here is the code,
What I have tried:
//////Row color changing here///////
public void _timer_Tick(object sender, EventArgs e)
{
try
{
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;
}
}
}
private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
{
try
{
var senderGrid = (DataGridView)sender;
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.Aqua;
}
row.Cells[SerialNo.Name].Value = false;
}
if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
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();
var r = this.dataGridView1.Rows[e.RowIndex];
r.DefaultCellStyle.BackColor = Color.Aqua;
}
ifile.IniWriteValue("Config Section", "INDENT_NO", data);
txtindentcode.Text = data;
}
}
}
}
EDITED SOLUTION :
by changing code as follows timer event is same as above changes is in only dgv event,
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)
{
for (int k = 0; k <= row.Cells.Count - 1; k++)
{
if (row.Cells[k].Style.BackColor == Color.Aqua)
{
for (int n = 0; n <= row.Cells.Count - 1; n++)
{
row.Cells[n].Style.BackColor = Color.White;
}
}
}
row.Cells[SerialNo.Name].Value = false;
}
if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
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];
for (int n = 0; n <= r.Cells.Count - 1; n++)
{
r.Cells[n].Style.BackColor = Color.Aqua;
}
}
//Below code for getting checked value of checkbox
ifile.IniWriteValue("Config Section", "INDENT_NO", data);
txtindentcode.Text = data;
}
}
///summary///
///for button column///
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 == false)
{
if (networkStream != null)
{
isButtonClicked = true;
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 { } // flag withoutprocess
}
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
{}
}