change color of row in a Datagrid view.

K

kjqua

Hi all,

I am newbie in c# and i have a question regarding DataGridView


I have a Table with 3 Columns
(0)IdLeggiSeriale <Is Primary key>
(1)ValoreSeriale <Is Integer>
(2)DataAcquisizione <Is DateTime>


I will like to change the color of each row that
in the in column(1) "ValoreSeriale" the Value is greater than 50.


private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{

if
(dataGridView1.Columns[e.ColumnIndex].Name.Equals("ValoreSeriale"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue > 50))
{
e.CellStyle.BackColor = Color.Yellow;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
}

In the Above code i only change the color a singular cell.

How can i change all the row.

Thanks
Marco
 
P

Puja

hi

u could use below function and call this function OnRowCreated=HighlightRow
in your grid view

protected void HighlightRow(Object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

//find your control here whether its a textbox or label

Label lblValoreSeriale= (Label)e.Row.FindControl("lblValoreSeriale");

if(lblValorSeriale != null)

{

ifConvert.ToInt32((lblValoreSeriale.Text) > 50)

e.Row.BackColor = System.Drawing.Color.Aqua;

}



}

}

hope this helps
 
K

kjqua

Hi Puja,

I try your function but i have this error:

The type or namespace name 'GridViewRowEventArgs' could not be found
(are you missing a using directive or an assembly reference?)

The dataGridView1 is on the form.

LeggiSeriale.mdf is the name of my database

Seriale_1 Is the name of the Table
IdLeggiSeriale is the name of the 1 column in the Table Seriale_1
ValoreSeriale is the name of the 2 column in the Table Seriale_1
DataAcquisizione is the name of the 3 column in the Table Seriale_1

thanks


Puja ha scritto:
hi

u could use below function and call this function OnRowCreated=HighlightRow
in your grid view

protected void HighlightRow(Object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

//find your control here whether its a textbox or label

Label lblValoreSeriale= (Label)e.Row.FindControl("lblValoreSeriale");

if(lblValorSeriale != null)

{

ifConvert.ToInt32((lblValoreSeriale.Text) > 50)

e.Row.BackColor = System.Drawing.Color.Aqua;

}



}

}

hope this helps


kjqua said:
Hi all,

I am newbie in c# and i have a question regarding DataGridView


I have a Table with 3 Columns
(0)IdLeggiSeriale <Is Primary key>
(1)ValoreSeriale <Is Integer>
(2)DataAcquisizione <Is DateTime>


I will like to change the color of each row that
in the in column(1) "ValoreSeriale" the Value is greater than 50.


private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{

if
(dataGridView1.Columns[e.ColumnIndex].Name.Equals("ValoreSeriale"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue > 50))
{
e.CellStyle.BackColor = Color.Yellow;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
}

In the Above code i only change the color a singular cell.

How can i change all the row.

Thanks
Marco
 
P

Puja

are u using dot net version 2.0? i am using same code and it works fine.



kjqua said:
Hi Puja,

I try your function but i have this error:

The type or namespace name 'GridViewRowEventArgs' could not be found (are
you missing a using directive or an assembly reference?)

The dataGridView1 is on the form.

LeggiSeriale.mdf is the name of my database

Seriale_1 Is the name of the Table
IdLeggiSeriale is the name of the 1 column in the Table Seriale_1
ValoreSeriale is the name of the 2 column in the Table Seriale_1
DataAcquisizione is the name of the 3 column in the Table Seriale_1

thanks


Puja ha scritto:
hi

u could use below function and call this function
OnRowCreated=HighlightRow in your grid view

protected void HighlightRow(Object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

//find your control here whether its a textbox or label

Label lblValoreSeriale= (Label)e.Row.FindControl("lblValoreSeriale");

if(lblValorSeriale != null)

{

ifConvert.ToInt32((lblValoreSeriale.Text) > 50)

e.Row.BackColor = System.Drawing.Color.Aqua;

}



}

}

hope this helps


kjqua said:
Hi all,

I am newbie in c# and i have a question regarding DataGridView


I have a Table with 3 Columns
(0)IdLeggiSeriale <Is Primary key>
(1)ValoreSeriale <Is Integer>
(2)DataAcquisizione <Is DateTime>


I will like to change the color of each row that
in the in column(1) "ValoreSeriale" the Value is greater than 50.


private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{

if
(dataGridView1.Columns[e.ColumnIndex].Name.Equals("ValoreSeriale"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue > 50))
{
e.CellStyle.BackColor = Color.Yellow;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
}

In the Above code i only change the color a singular cell.

How can i change all the row.

Thanks
Marco
 

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

Top