Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView help
 

DataGridView help

Hey guys,

I need your help with 2 things on a DataGridView in VS2005.

1) I need a cell to have 1 row in it underlined, e.g:

hello

world

2) I need to have a border between 2 lines in the DataGridView highlighted (bolded, colored etc)

Thanks!

AvivG  Wednesday, December 05, 2007 12:35 AM

You can handle the CellPainting event to draw the style you want, something like this


Code Block

void Form4_Load(object sender, EventArgs e)

{

this.dataGridView1.Columns.Add("c1", "c1");

this.dataGridView1.Columns.Add("c2", "c2");

this.dataGridView1.Columns.Add("c3", "c3");

this.dataGridView1.Rows.Add("Hello\nWorld", "bbb", "ccc");

this.dataGridView1.Rows.Add();

this.dataGridView1.Rows[0].Height = this.dataGridView1.RowTemplate.Height * 2;

this.dataGridView1.CellPainting +=

new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == 0 && e.ColumnIndex > -1)

{

e.Handled = true;

e.PaintBackground(e.CellBounds,true);

if (e.ColumnIndex != 0)

{

e.PaintContent(e.CellBounds);

}

using (Pen pen = new Pen(Color.Red, 2))

{

e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

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

}

if (e.ColumnIndex == 0)

{

Rectangle rect = e.CellBounds;

rect.Height /= 2;

using (Font f = new Font(e.CellStyle.Font, FontStyle.Underline))

using (SolidBrush br = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString("Hello", f, br, rect);

rect.Y += rect.Height;

e.Graphics.DrawString("World", e.CellStyle.Font, br, rect);

}

}

}

}

Check out this recent thread.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2502612&SiteID=1

Rudedog

Rudedog2  Wednesday, December 05, 2007 12:47 AM

Hi,

For the first question, do you want to set the text of a cell with different font style?

For example, if the cell text is “hello word� you want to set “hello�underlined, and “world�normal? Isn’t it?

For the second question, what do you mean “border between 2 lines� Does it mean the border between two rows?

Thanks!

Yan-Fei Wei  Thursday, December 06, 2007 8:09 AM

Hi,

Thanks. Yes I want to have 2 different styles in one cell.

And for the second question, yes I meant a border between 2 rows. I have a grid that shows scores and I need to highlight the average boundry between 2 rows.

Thanks!

AvivG  Friday, December 07, 2007 12:24 AM

You can handle the CellPainting event to draw the style you want, something like this


Code Block

void Form4_Load(object sender, EventArgs e)

{

this.dataGridView1.Columns.Add("c1", "c1");

this.dataGridView1.Columns.Add("c2", "c2");

this.dataGridView1.Columns.Add("c3", "c3");

this.dataGridView1.Rows.Add("Hello\nWorld", "bbb", "ccc");

this.dataGridView1.Rows.Add();

this.dataGridView1.Rows[0].Height = this.dataGridView1.RowTemplate.Height * 2;

this.dataGridView1.CellPainting +=

new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == 0 && e.ColumnIndex > -1)

{

e.Handled = true;

e.PaintBackground(e.CellBounds,true);

if (e.ColumnIndex != 0)

{

e.PaintContent(e.CellBounds);

}

using (Pen pen = new Pen(Color.Red, 2))

{

e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

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

}

if (e.ColumnIndex == 0)

{

Rectangle rect = e.CellBounds;

rect.Height /= 2;

using (Font f = new Font(e.CellStyle.Font, FontStyle.Underline))

using (SolidBrush br = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString("Hello", f, br, rect);

rect.Y += rect.Height;

e.Graphics.DrawString("World", e.CellStyle.Font, br, rect);

}

}

}

}

You can use google to search for other answers

Custom Search

More Threads

• how to insert image into data base other than file streams
• Datagrid Refresh
• DataGridView Issue with AutoGenerateColumns
• SqlDataAdapter Fill problem with multiple tables
• Pop up control in datagridview????
• Error Publishing Website on NET
• How to integrate a combobox conrol in datagridview control
• Horrible selection performance with FullRowSelect
• Bindingsource AddingNew causes "Cannot add external objects to this list"
• Index 0 is either negative or above rows count.