The blog has moved to a new address. The blog is now located at http://devintelligence.com

Adsense

Sunday, February 18, 2007

Change font style for specific cell in datagridview

The way to change font for a single cell in an unbound datagridview.


 



int columnIndex = 1;
int rowIndex = 2;
// create bold font based on the default font
Font newFont = new Font(dataGridView1.Font, FontStyle.Bold);
dataGridView1[columnIndex, rowIndex].Style.Font
= newFont;


 


Using this simple technique you can change foreground or background color ( and many more style related properties ) for specific cell .

11 comments:

zawmn said...

I want to change the background color of specific cell with pattern (e.g. hatch style) when I click on this cell.

How can I do it?

Anonymous said...

Thanks for the great code, I have search lot's of sites but unable to found the desire O/P. But your code is excellent Thanks once again...

Anonymous said...

Hi

Great Code
Thanks a lot.


Anish.M.N

Anonymous said...

Thanks... Exactly what I am looking for...

Unknown said...

Just wanted to add that when doing this in say your DataBindingComplete event, and have a large number of rows, you should have AutoResizeColumn mode set to false. Failure to doing this will result in a large performance hit.

Saurabh Goyal said...

Hi,

Your code is great.

But i didnt find the way to change the specific column font at one shot only.
I have binded datagridview using binding source and data table.

Here is my code... The last line of my code is not working.
Please help me... Thanks in advance.


BindingSource bs = new BindingSource();
OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery, myConnection);
DataTable dt = new DataTable();
dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
da.Fill(dt);

dt.Columns[0].ColumnName = "Record Id";
dt.Columns[1].ColumnName = "Customer Code";
dt.Columns[2].ColumnName = "Customer Name";
dt.Columns[3].ColumnName = "Address 1";
dt.Columns[4].ColumnName = "Address 2";
dt.Columns[5].ColumnName = "No Of T.V.";
dt.Columns[6].ColumnName = "Subscription Amount";
dt.Columns[7].ColumnName = "Subscription Period";
dt.Columns[8].ColumnName = "Collection Boy Id";

bs.DataSource = dt;
dgvCustomer.DataSource = bs;

dgvCustomer.Columns[0].Visible = false;
dgvCustomer.Columns[2].DefaultCellStyle.Font = new Font("SHREE-DEV-0726-S02", 12, FontStyle.Regular);

Taras said...

@Saurabh
Take a look at the code ,that I used change style for a column.It works.

dataGridView1.Columns[2].DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Bold);

Probably you need to provide a normal FontFamily name ,something like "Arial"(as I did) instead "SHREE-DEV-0726-S02" .

anurupr said...

i used your code... and its great.. but the problem the changes occur only after i click onto another cell which is kind of weird and is not really what i wanted .. is there anyway to make sure the changes occur that time itself

Reza said...

Saurabh Thanks for help me........

Reza said...

Saurabh ,
thanks a lot for saving time and annoying....

Anonymous said...

yes i agree with every one that this is and awesome solution for me ...it help me gr8