color one cell in DataGridView

  • Thread starter Thread starter Jassim Rahma
  • Start date Start date
J

Jassim Rahma

I am using this code to populate data into datagrid..

private void get_hosts()
{
data_table = new DataTable();

sql_connection = new SqlCeConnection("Data Source=" + data_file
+ ";Password=xxx");
sql_connection.Open();

strSQL = "SELECT host_id, host_title, host_name, host_disabled
FROM hosts ORDER BY host_title";
sql_command = new SqlCeCommand(strSQL, sql_connection);
sql_adapter = new SqlCeDataAdapter(sql_command);
sql_adapter.Fill(data_table);
dataHosts.DataSource = data_table;

dataHosts.Columns["host_id"].HeaderText = "host ID";
dataHosts.Columns["host_id"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopRight;
dataHosts.Columns["host_id"].Width = 120;
dataHosts.Columns["host_id"].Visible = false;

dataHosts.Columns["host_title"].HeaderText = "host title";
dataHosts.Columns["host_title"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_title"].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_title"].Width = 320;

dataHosts.Columns["host_name"].HeaderText = "host name";
dataHosts.Columns["host_name"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_name"].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_name"].Width = 320;

dataHosts.Columns["host_disabled"].HeaderText = "disabled?";
dataHosts.Columns["host_disabled"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopCenter;
dataHosts.Columns["host_disabled"].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.TopCenter;
dataHosts.Columns["host_disabled"].Width = 80;

sql_connection.Close();
}


and i want to color the cell host_disabled if the value = 'N'
 
Hi Jassim,
I think you need to override the paint method of the cell.
Ken Getz has an article in Jul/Aug issue of Code Magazine that should
provide the basis for a solution. The following code is for an event
handler for the Datagrid prepaint event.
It colours the whole row (if it is selected) depending on a cell
value.
I adapted the code from an example (can't remember where so my
apologies to the original author)
HTH
Bob

void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
const int iDoRead = 7;
const int iCompId = 6;

// Do not automatically paint the focus rectangle.
//e.PaintParts &= ~DataGridViewPaintParts.Focus;

// Determine whether the cell should be painted
// with the custom selection background.
//if ((e.State & DataGridViewElementStates.Selected) ==
// DataGridViewElementStates.Selected)
{
// Calculate the bounds of the row.
Rectangle rowBounds = new Rectangle(
dataGridView1.RowHeadersWidth, e.RowBounds.Top,
dataGridView1.Columns.GetColumnsWidth(
DataGridViewElementStates.Visible) -
dataGridView1.HorizontalScrollingOffset + 1,
e.RowBounds.Height);

// Paint the custom selection background.
//dataGridView1.DefaultCellStyle.SelectionBackColor,
//e.InheritedRowStyle.ForeColor,
//using (Brush backbrush =
// new
System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
// Color.DarkRed,
// e.InheritedRowStyle.ForeColor,
//
System.Drawing.Drawing2D.LinearGradientMode.Horizontal))


{
Brush backbrush;
int i = e.RowIndex;
DataGridViewRow r =
((DataGridView)sender).Rows;
int iRead = (int) r.Cells[iDoRead].Value;
int iCompanyId = (int) r.Cells[iCompId].Value;
switch (iRead)
{
case 0:

backbrush = new
System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
Color.Red,
e.InheritedRowStyle.
ForeColor,
System.Drawing.
Drawing2D.
LinearGradientMode.
Horizontal);
break;
case 1:
if (iCompanyId == 7 || iCompanyId == 2 ||
iCompanyId == 3 || iCompanyId == 4 ||
iCompanyId == 6)
backbrush = new
System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
Color.Cyan,
e.InheritedRowStyle.
ForeColor,
System.Drawing.
Drawing2D.
LinearGradientMode.
Horizontal);
else
backbrush = new
System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
Color.Yellow,
e.InheritedRowStyle.
ForeColor,
System.Drawing.
Drawing2D.
LinearGradientMode.
Horizontal);

break;


default:
backbrush = new
System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
Color.DeepPink,
e.InheritedRowStyle.
ForeColor,
System.Drawing.
Drawing2D.
LinearGradientMode.
Horizontal);
break;
}


e.Graphics.FillRectangle(backbrush, rowBounds);
}
}
}





I am using this code to populate data into datagrid..

private void get_hosts()
{
data_table = new DataTable();

sql_connection = new SqlCeConnection("Data Source=" + data_file
+ ";Password=xxx");
sql_connection.Open();

strSQL = "SELECT host_id, host_title, host_name, host_disabled
FROM hosts ORDER BY host_title";
sql_command = new SqlCeCommand(strSQL, sql_connection);
sql_adapter = new SqlCeDataAdapter(sql_command);
sql_adapter.Fill(data_table);
dataHosts.DataSource = data_table;

dataHosts.Columns["host_id"].HeaderText = "host ID";
dataHosts.Columns["host_id"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopRight;
dataHosts.Columns["host_id"].Width = 120;
dataHosts.Columns["host_id"].Visible = false;

dataHosts.Columns["host_title"].HeaderText = "host title";
dataHosts.Columns["host_title"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_title"].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_title"].Width = 320;

dataHosts.Columns["host_name"].HeaderText = "host name";
dataHosts.Columns["host_name"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_name"].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.TopLeft;
dataHosts.Columns["host_name"].Width = 320;

dataHosts.Columns["host_disabled"].HeaderText = "disabled?";
dataHosts.Columns["host_disabled"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopCenter;
dataHosts.Columns["host_disabled"].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.TopCenter;
dataHosts.Columns["host_disabled"].Width = 80;

sql_connection.Close();
}


and i want to color the cell host_disabled if the value = 'N'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top