Mathew Philip Palliampil

How to change the column color for a particular datagrid column

( not only for current cell but also for the entire column )

See the following code.... it will work (change color) only when we click in the column cell..but i need to change the entire columns color.. how to do

DataGridTableStyle dataGridTableStyle1 = new DataGridTableStyle();

dataGridTableStyle1.MappingName = "TABLE_NAME"; //

DataGridTextBoxColumn dataGridTextBoxColumn1 = new DataGridTextBoxColumn();

dataGridTextBoxColumn1.MappingName = "table_field1"; //TABLE FIELD NAME

dataGridTextBoxColumn1.HeaderText = "Headder_Text ";

dataGridTextBoxColumn1.Width = 150;

dataGridTextBoxColumn1.ReadOnly = false;

dataGridTextBoxColumn1.Format = "0.000";

dataGridTextBoxColumn1.Alignment = System.Windows.Forms.HorizontalAlignment.Right;

dataGridTextBoxColumn1.NullText = "0.000";

dataGridTextBoxColumn1.TextBox.BackColor = Color.Yellow;

dataGridTableStyle1.GridColumnStyles.Add(dataGridTextBoxColumn1);

dataGridStock.TableStyles.Add(dataGridTableStyle1); //ADDING CUSTOM STYLE TO DATAGRID

Any body knows please help..



Re: Windows Forms General Datagrid Column color

Zhi-Xin Ye - MSFT

You have to sub-class the datagridtextboxColumn, i.e, make your own DataGridViewTextboxColumn inherited from DataGridViewTextboxColumn class, override the Paint event,

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

namespace WindowsApplication1

{

public class Form3 : System.Windows.Forms.Form

{

private void Form3_Load(object sender, System.EventArgs e)

{

//other codes here

DataGridTableStyle tbStyle = new DataGridTableStyle();

myColumnStyle dataGridTextBoxColumn1 = new myColumnStyle();

dataGridTextBoxColumn1.MappingName = "bookname"; //TABLE FIELD NAME

dataGridTextBoxColumn1.HeaderText = "name ";

dataGridTextBoxColumn1.Width = 150;

dataGridTextBoxColumn1.ReadOnly = false;

dataGridTextBoxColumn1.TextBox.BackColor = Color.Yellow;

tbStyle.GridColumnStyles.Add(dataGridTextBoxColumn1);

this.dataGrid1.TableStyles.Add(tbStyle);

}

}

public class myColumnStyle:DataGridTextBoxColumn

{

protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)

{

string text = this.GetColumnValueAtRow(Source,RowNum).ToString();

this.PaintText(g, Bounds,text,Brushes.Yellow,Brushes.Black,false);

}

}

}

Hope it helps.
Best Regards.
Zhixin Ye






Re: Windows Forms General Datagrid Column color

Mathew Philip Palliampil

Yes helpful