Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

change color of row in a Datagrid view.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kjqua - 04 Jul 2007 06:51 GMT
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
Puja - 04 Jul 2007 07:40 GMT
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

> Hi all,
>
[quoted text clipped - 31 lines]
> Thanks
> Marco
kjqua - 04 Jul 2007 08:20 GMT
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
>
[quoted text clipped - 64 lines]
>> Thanks
>> Marco
Puja - 05 Jul 2007 01:41 GMT
are u using dot net version 2.0? i am using same code and it works fine.

> Hi Puja,
>
[quoted text clipped - 83 lines]
>>> Thanks
>>> Marco

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.