HI Friends,
I have some problem in winform datagridcombobox using. The prob is I m using 2 combobox inside the datagrid for 1st combobox i m displaying imagecolour and combobox country name. To fill images to 1st combobox i m using DrawItem event. The problem is this event applying to both combobox. how to avoid this conflict.
private void dgvMenuOptions_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmbBox;
TextBox textDescription;
if (e.Control is ComboBox && dgvMenuOptions.CurrentCell.ColumnIndex == 5)
{
if (cmbBox != null)
{
cmbcolour.DrawMode = DrawMode.OwnerDrawFixed;
cmbcolour.DrawItem -= new DrawItemEventHandler(ComboBox1_DrawItem);
cmbcolour.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
}
}
}
private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
System.Drawing.Color animalColor = new System.Drawing.Color();
if (e.Index != -1)
{
//string Colour_Code = DBMenu.GetColourCode(color1);
Color Fontcolor = (Color)new ColorConverter().ConvertFromString(ImageColourCode[e.Index].ToString());
//"Blue", "Green", "Orange", "Purple", "Red", "Yellow" };
switch (e.Index)
{
case 0:
brush = Brushes.BlueViolet;
break;
case 1:
brush = Brushes.GreenYellow;
break;
case 2:
brush = Brushes.Orange;
break;
case 3:
brush = Brushes.Purple;
break;
case 4:
brush = Brushes.Red;
break;
case 5:
brush = Brushes.Yellow;
break;
}
// Draw the background of the item.
e.DrawBackground();
// Create a square filled with the animals color. Vary the size
// of the rectangle based on the length of the animals name.
//Rectangle rectangle = new Rectangle(1, e.Bounds.Top + 2, e.Bounds.Height, e.Bounds.Height - 4);
Rectangle rectangle = new Rectangle();
rectangle.X = e.Bounds.X + 2;
rectangle.Y = e.Bounds.Y + 2;
rectangle.Width = 20;
rectangle.Height = e.Bounds.Height - 3;
e.Graphics.FillRectangle(new SolidBrush(Fontcolor), rectangle);
// Draw each string in the array, using a different size, color, g.DrawRectangle(pen, 100,100, 100, 200);
// and font for each item.
// size = 10;
animalColor = System.Drawing.Color.Blue;
// family = FontFamily.GenericSansSerif;
System.Drawing.Font myFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
e.Graphics.DrawString(ImageColour[e.Index], myFont, brush, new RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
// e.Graphics.DrawImage(System.Drawing.Image.FromFile(ImagefilePath[e.Index]), rectangle);
// Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}
}private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
System.Drawing.Color animalColor = new System.Drawing.Color();
if (e.Index != -1)
{
//string Colour_Code = DBMenu.GetColourCode(color1);
Color Fontcolor = (Color)new ColorConverter().ConvertFromString(ImageColourCode[e.Index].ToString());
//"Blue", "Green", "Orange", "Purple", "Red", "Yellow" };
switch (e.Index)
{
case 0:
brush = Brushes.BlueViolet;
break;
case 1:
brush = Brushes.GreenYellow;
break;
case 2:
brush = Brushes.Orange;
break;
case 3:
brush = Brushes.Purple;
break;
case 4:
brush = Brushes.Red;
break;
case 5:
brush = Brushes.Yellow;
break;
}
// Draw the background of the item.
e.DrawBackground();
// Create a square filled with the animals color. Vary the size
// of the rectangle based on the length of the animals name.
//Rectangle rectangle = new Rectangle(1, e.Bounds.Top + 2, e.Bounds.Height, e.Bounds.Height - 4);
Rectangle rectangle = new Rectangle();
rectangle.X = e.Bounds.X + 2;
rectangle.Y = e.Bounds.Y + 2;
rectangle.Width = 20;
rectangle.Height = e.Bounds.Height - 3;
e.Graphics.FillRectangle(new SolidBrush(Fontcolor), rectangle);
// Draw each string in the array, using a different size, color, g.DrawRectangle(pen, 100,100, 100, 200);
// and font for each item.
// size = 10;
animalColor = System.Drawing.Color.Blue;
// family = FontFamily.GenericSansSerif;
System.Drawing.Font myFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
e.Graphics.DrawString(ImageColour[e.Index], myFont, brush, new RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
// e.Graphics.DrawImage(System.Drawing.Image.FromFile(ImagefilePath[e.Index]), rectangle);
// Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}
}