Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Change the boder colour in datagridview
 

Change the boder colour in datagridview

hi,

How to change the boder color ofa column in the datagridview and too in left side only and not in all 4 sides.... Is it possible if yes means how...?

seewinpgm  Thursday, June 19, 2008 10:28 AM

Hi kabilan1,

Based on my understanding, what you want to do is to draw the grid with a different color when the left button of the mouse is pressed, right? If so, you can handle the MouseMove event of the DataGridView and check the left button is pressed to determine whether to custom draw the grid. Here is a sample for your information:

Code Snippet

this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

this.dataGridView1.MultiSelect = false;

this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

this.dataGridView1.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);

int columnIndex = -2;

int rowIndex = -2;

void dataGridView1_MouseMove(object sender, MouseEventArgs e)

{

DataGridView.HitTestInfo info = this.dataGridView1.HitTest(e.X, e.Y);

if (e.Button == MouseButtons.Left)

{

columnIndex = info.ColumnIndex;

rowIndex = info.RowIndex;

this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex);

if (info.ColumnIndex - 1 >= -1)

this.dataGridView1.InvalidateCell(info.ColumnIndex - 1, info.RowIndex);

if (info.ColumnIndex + 1 <= this.dataGridView1.ColumnCount - 1)

this.dataGridView1.InvalidateCell(info.ColumnIndex + 1, info.RowIndex);

if (info.RowIndex - 1 >= -1)

this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex - 1);

if (info.RowIndex + 1 < this.dataGridView1.NewRowIndex)

this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex + 1);

}

else

{

columnIndex = -2;

rowIndex = -2;

}

}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.ColumnIndex == columnIndex && e.RowIndex == rowIndex)

{

using (

Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor))

{

using (Pen gridLinePen = new Pen(gridBrush))

{

// Erase the cell.

e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

// Draw the grid lines (only the right and bottom lines;

// DataGridView takes care of the others).

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,

e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,

e.CellBounds.Bottom - 1);

e.Graphics.DrawLine(Pens.Blue, e.CellBounds.Right - 1,

e.CellBounds.Top, e.CellBounds.Right - 1,

e.CellBounds.Bottom);

// Draw the text content of the cell, ignoring alignment.

if (e.Value != null)

{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + 2,

e.CellBounds.Y + 2, StringFormat.GenericDefault);

}

e.Handled = true;

}

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Wednesday, June 25, 2008 10:53 AM

Hi,

You can override DataGridView.OnPaint method and paint the DataGridView (including the columns you want to customize) by yourself. Here's a reference to the method: DataGridView.OnPaint Method (System.Windows.Forms)

Regards,

Jacob

Jacob Sui - MSFT  Monday, June 23, 2008 7:18 AM
hi,

ok i used DataGridView_CellPainting event for painting but wat i exactly want is when i drag a column from treeview to datagridview and while before droping the column should display with the line as i aske above. Example If there are 4 column in datagridview am draging a new column to the datagridview and i want to while draging the on a column1 i want to show the blue line before the column1, similarly when i move over the column2 by dragging a new column the same line should shown before the column2 and column1 line should be disable... Is it possible ? if yes how...?

The action should be perform in left mouse down even.... If any coding or refference is there plz give urgent...
seewinpgm  Monday, June 23, 2008 2:23 PM

Hi kabilan1,

Based on my understanding, what you want to do is to draw the grid with a different color when the left button of the mouse is pressed, right? If so, you can handle the MouseMove event of the DataGridView and check the left button is pressed to determine whether to custom draw the grid. Here is a sample for your information:

Code Snippet

this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

this.dataGridView1.MultiSelect = false;

this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

this.dataGridView1.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);

int columnIndex = -2;

int rowIndex = -2;

void dataGridView1_MouseMove(object sender, MouseEventArgs e)

{

DataGridView.HitTestInfo info = this.dataGridView1.HitTest(e.X, e.Y);

if (e.Button == MouseButtons.Left)

{

columnIndex = info.ColumnIndex;

rowIndex = info.RowIndex;

this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex);

if (info.ColumnIndex - 1 >= -1)

this.dataGridView1.InvalidateCell(info.ColumnIndex - 1, info.RowIndex);

if (info.ColumnIndex + 1 <= this.dataGridView1.ColumnCount - 1)

this.dataGridView1.InvalidateCell(info.ColumnIndex + 1, info.RowIndex);

if (info.RowIndex - 1 >= -1)

this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex - 1);

if (info.RowIndex + 1 < this.dataGridView1.NewRowIndex)

this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex + 1);

}

else

{

columnIndex = -2;

rowIndex = -2;

}

}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.ColumnIndex == columnIndex && e.RowIndex == rowIndex)

{

using (

Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor))

{

using (Pen gridLinePen = new Pen(gridBrush))

{

// Erase the cell.

e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

// Draw the grid lines (only the right and bottom lines;

// DataGridView takes care of the others).

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,

e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,

e.CellBounds.Bottom - 1);

e.Graphics.DrawLine(Pens.Blue, e.CellBounds.Right - 1,

e.CellBounds.Top, e.CellBounds.Right - 1,

e.CellBounds.Bottom);

// Draw the text content of the cell, ignoring alignment.

if (e.Value != null)

{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + 2,

e.CellBounds.Y + 2, StringFormat.GenericDefault);

}

e.Handled = true;

}

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Wednesday, June 25, 2008 10:53 AM

You can use google to search for other answers

Custom Search

More Threads

• Win XP style in designer window of VS.Net 2003
• Problem with form nesting!
• how to make form dragable when FormBorderStyle is set to none
• Change Smart Tag contents on the fly?
• Add a Windows Form file into a project
• Subclassing toolStripButton
• Designer cannot be shown VC++ 2005
• DataGridView SelectionChanged event fired 3 times
• Printing / PrintDialog Problem - "Attempting to read or write protected memory."
• sql server name