The following code is designed to setup a DataGridView object in c# .net. It will populate the object and set the 'Selected Cell' fore/back colour to transparent. When a particular row is double clicked, if that row is not set, it should change the background colour to green. If that row is set, it should change the background colour to white.
two problems arise:
1) Initially the first row of the datagrid is highlighted. it is not transparent but solid white. Clicking a different row will fix this and the next selected row is transparent.
2) When a double click has been performed, the datagrid does not change the background of that row to its new colour. It will not change colour unless another row is selected.
I want the background colour change to be instant on the double click operation.
I also want the initial selected row to be transparent.
Can my code be fixed, or is there a better method to attain the desired effect.
Below is a simplified extract of my code.
Create a c# .net project, blank form.
Create a DataGridView object - do not set any parameters via the properties page. Use the following function to be called in the form constructor to setup the table:
const int TotalDummyData = 20;
/// <summary>
///
/// </summary>
private void CreateDGV()
{
dgv.Width = 420;
dgv.Columns.Add("h1", "Name");
dgv.Columns.Add("h2", "Value");
dgv.Columns[0].Width = 200;
dgv.Columns[1].Width = 200;
dgv.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgv.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
dgv.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
for (int i = 0; i < TotalDummyData; i++)
{
DataGridViewRow dgvRow = new DataGridViewRow();
DataGridViewCell dgvCell1 = new DataGridViewTextBoxCell();
DataGridViewCell dgvCell2 = new DataGridViewTextBoxCell();
dgvCell1.Value = "dummy_data_name_" + i.ToString();
dgvCell2.Value = "dummy_data_value_" + i.ToString();
dgvRow.Cells.Add(dgvCell1);
dgvRow.Cells.Add(dgvCell2);
dgv.Rows.Add(dgvRow);
}
dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgv.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 12.0f, FontStyle.Bold);
dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.AllowUserToResizeColumns = false;
dgv.AllowUserToResizeRows = false;
dgv.ReadOnly = true;
dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
dgv.RowHeadersVisible = false;
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.DefaultCellStyle.SelectionBackColor = Color.Transparent;
dgv.DefaultCellStyle.SelectionForeColor = Color.Transparent;
dgv.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgv_CellContentDoubleClick);
}
Now create the cell double click function - this will change the cell background colour
bool[] DummyDataSet = new bool[20];
/// <summary>
///
/// </summary>
/// <param name ="sender"></param>
/// <param name ="e"></param>
private void dgv_CellContentDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if(!DummyDataSet[e.RowIndex])
{
DummyDataSet[e.RowIndex] = true;
dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
}
else
{
DummyDataSet[e.RowIndex] = false;
dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
}
}