Results 1 to 6 of 6

Thread: Change Color of a cell of Gridview control in Asp.Net

  1. #1
    Join Date
    Dec 2008
    Posts
    9

    Change Color of a cell of Gridview control in Asp.Net

    Hi,

    I want to change the cell color in gridview in my asp.net project. but i don't know how to change the color in runtime.

    If you have any idea about this please help me.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,856

    Re: Change Color of a cell of Gridview control in Asp.Net

    In RowDataBound event of GridView, you can do that.

    use this code,

    Code:
      protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Gray;
            }
        }
    Same can do with datagrid in Item databound event.

    Hope this helps you.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Change Color of a cell of Gridview control in Asp.Net

    You can use a template or you can handle the rowdatabound event. if you handle the event, you can simply change the background of the cell with code similar to this one:

    void Handle( object sender, GriViewRowEventArgs args )
    {
    args.Row.Cells[put the position of the cell here].BackColor = your color here;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,015

    Re: Change Color of a cell of Gridview control in Asp.Net

    You can refer to http://msdn.microsoft.com/en-us/library/aa479342.aspx for more information about gridview control.

  5. #5
    Join Date
    Apr 2008
    Posts
    2,010

    Re: Change Color of a cell of Gridview control in Asp.Net

    If you want to change the color of selected row or cell then,
    try the below one...

    Code:
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
            
          GridView1.SelectedRow.Cells(4).BackColor = Drawing.Color.Red
    
    End Sub

  6. #6
    Join Date
    Dec 2010
    Posts
    1

    Re: Change Color of a cell of Gridview control in Asp.Net

    Useful Materials. They works for me. Thanks a lot

Similar Threads

  1. Replies: 3
    Last Post: 18-03-2012, 07:33 PM
  2. Does it is possible to have color shading in a Count if cell
    By Rounder1 in forum MS Office Support
    Replies: 2
    Last Post: 16-02-2012, 05:37 PM
  3. How to change cell color in excel based on text input
    By Chini mao in forum Windows Software
    Replies: 2
    Last Post: 06-01-2012, 08:19 PM
  4. How to put background color for html cell
    By Preetish in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 03:32 PM
  5. Excel 2000 - Change Default Cell Fill Color
    By Cris Han in forum MS Office Support
    Replies: 2
    Last Post: 20-02-2008, 08:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •