Datagrid control

Discussion in 'ASP .Net' started by Paul, Apr 28, 2004.

  1. Paul

    Paul Guest

    I have a datagrid I want to change the color of the datagrid row dependant
    upon the contents of the data in the row after I bind it to the dataset.

    Any hints to how to do this would be appreciated.

    Thanks
     
    Paul, Apr 28, 2004
    #1
    1. Advertising

  2. this may help

    public static void SetDataGridColors(ref DataGrid dg)
    {
    int itemCount = dg.Items.Count;
    for(int 1=0;1<itemCount;i++)
    {
    string myCellValue = dg.Items.Cells[0].Text();
    switch(myCellValue)
    {
    case "this value"
    dg.Items.BackColor = System.Drawing.Color.Red;
    break;
    case "that value"
    dg.Items.BackColor = System.Drawing.Color.White;
    break;
    }
    }
    }

    this will change the color of the entire row based on the value of the 1st cell in each row.
    Remember the cells collection of the datarow is 0 based

    if you want to change only one cell in the row a second loop will be needed on the cells collection in the datagrid items loop

    hth
    Harold
     
    =?Utf-8?B?aGFyb2xk?=, Apr 28, 2004
    #2
    1. Advertising

  3. sorry paul

    You call it by sending the completed grid to it.
    It does not have to be on ItemDataBound event of the grid, as our helpful friend Diogo said, but it does have to be after it is databound. You may want to change the color of things in the grid if you have edits to the grid without rebinding. This will fire on postback and the ItemDataBound only fires when it's bound up
    Like

    datagrid1.DataBind()

    SetGridColors(ref datagrid1
     
    =?Utf-8?B?aGFyb2xk?=, Apr 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhuwan Bhaskar
    Replies:
    2
    Views:
    387
    Bhuwan Bhaskar
    Oct 4, 2007
  2. Jc Morin
    Replies:
    1
    Views:
    187
    Jc Morin
    Dec 5, 2003
  3. debartsa

    Composite Control not receiving DataGrid (child control) events?

    debartsa, Apr 19, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    186
    debartsa
    Apr 19, 2004
  4. debartsa
    Replies:
    1
    Views:
    258
    John Saunders
    Apr 23, 2004
  5. Andreas Klemt
    Replies:
    0
    Views:
    309
    Andreas Klemt
    Oct 8, 2003
Loading...

Share This Page