By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

change color of row in a Datagrid view.

P: n/a
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
Jul 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <kj***@pippo.itwrote in message
news:eZ**************@TK2MSFTNGP02.phx.gbl...
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

Jul 4 '07 #2

P: n/a
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" <kj***@pippo.itwrote in message
news:eZ**************@TK2MSFTNGP02.phx.gbl...
>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

Jul 4 '07 #3

P: n/a
are u using dot net version 2.0? i am using same code and it works fine.

"kjqua" <kj***@pippo.itwrote in message
news:eJ**************@TK2MSFTNGP02.phx.gbl...
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" <kj***@pippo.itwrote in message
news:eZ**************@TK2MSFTNGP02.phx.gbl...
>>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
Jul 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.