2009-08-25 10 views
5

Muszę sformatować (backcolor, forecolor, styl czcionki) komórki radgrid w zależności od wartości komórki.Jak programowo formatować komórkę radgrid

Na przykład Jeśli wartość jest ujemna, ustaw kolor przedniej komórki jako czerwony.

Czy ktoś może mi powiedzieć, jak można to osiągnąć?

Odpowiedz

4

Dodaj wiersz onItemDataBound = "Data_OnitemDataBound" do deklaracji radGrid na stronie aspx.

Następnie dodaj to do swojego kodu. Liczba w komórkach [] jest indeksem kolumny, którą chcesz zmodyfikować lub zweryfikować.

protected void Data_OnItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (e.Item is GridDataItem) 
    { 
     GridDataItem item = (GridDataItem)e.Item; 
     if (Convert.ToDecimal(item.Cells[3].Text) < 0) 
     { 
      item.Cells[3].ForeColor = System.Drawing.Color.Red; 
     } 
    } 
} 
7
protected void grdName_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
     if (e.Item is GridDataItem) 
     { 
      GridDataItem item = (GridDataItem)e.Item; 
      if (Convert.ToInt32(((DataRowView)item.DataItem)["Column"]) < value) 
      { 
       TableCell cell = item["Column"]; 
       cell.BackColor = Color.PeachPuff; 
      } 
     } 
    } 
+0

Nie wiem, dlaczego ktoś mnie zawiódł, moja odpowiedź różni się od drugiej, więc pomyślałem, że powinienem ją dodać. –

+0

Być może nie lubili PeachPuff – JohnnyBizzle

1

poniższy kod może być stosowany do wszystkich komórek w RadGrid.

protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
     foreach (GridDataItem dataItem in RadGridProduct.MasterTableView.Items) 
     { 
      int cellCount = dataItem.Cells.Count; 

      foreach (GridTableCell item in dataItem.Cells) 
      { 
       if (item.Text == null ||Convert.ToInt32(item.Text) < 0) 
        item.BackColor = System.Drawing.Color.Brown; 
      } 

     } 

    } 
Powiązane problemy