The idea here is that I redraw the combol "cell" so that it shows the block of colour and text. This is when form displays and it is about to show the dropdown:
After I have selected a colour it goes weird:
Now it is all wrong. I have to hover the mouse over the control to render other bits. Just not working right.
My handler:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.ClipBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, e.ClipBounds);
}
try
{
ComboboxColorItem oColorItem = (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = oColorItem.ToString();
c = oColorItem.Value;
}
catch
{
s = "red";
c = Color.Red;
}
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle(e.ClipBounds.Left + 5, e.ClipBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black, e.ClipBounds.Left + 25, e.ClipBounds.Top + 1);
b.Dispose();
g.Dispose();
e.Handled = true;
}
}
}
Is there something I am missing? Must be.
Update:
I tried this in the CellPainting event:
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
using (Graphics g = e.Graphics)
{
g.FillRectangle(Brushes.Aqua, e.CellBounds);
}
}
else
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
}
e.Handled = true;
That improves things in the sense that it does not go as weird. Ofcourse, it is not actually drawing anything. But then it doe snot take long for the left most cells (with the editing symbols) to only show in white. So the mechanics of it are still not right. Thank you.
If I try it the way suggested I end up with:
Made progress! Can we adkjust it to still include the grid lines? Like in normal cells?
After
ClipBounds
by CellBounds
g.Dispose();
..things look almost normal.
This is the result :
Of this Paint
event:
private void dataGridView2_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex == 0) // use your own checks here!!
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.CellBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, rDraw); // **
}
try
{ // use your own code here again!
// ComboboxColorItem oColorItem =
// (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = "WW";// oColorItem.ToString();
c = Color.LawnGreen;// oColorItem.Value;
} catch
{
s = "red";
c = Color.Red;
}
// asuming a square is right; make it a few pixels smaller!
int butSize = e.CellBounds.Height;
Rectangle rbut = new Rectangle(e.CellBounds.Right - butSize ,
e.CellBounds.Top, butSize , butSize );
ComboBoxRenderer.DrawDropDownButton(e.Graphics, rbut,
System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle( e.CellBounds.Left + 5,
e.CellBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black,
e.CellBounds.Left + 25, e.CellBounds.Top + 1);
b.Dispose();
//g.Dispose(); <-- do not dispose of thing you have not created!
e.Handled = true;
}
}
Note that I only have one CombBoxCell
, so I changed the checks. And that I have no ComboboxColorItem
, so I substituted a random string & color.
Update from OP: I had some of the syntax wrong and needed:
// use your own code here again!
if(DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
ComboboxColorItem oColorItem = (ComboboxColorItem)DataGridView1
.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
s = oColorItem.ToString();
c = oColorItem.Value;
}