Background of datagrid

J

Javier Pérez

Hello All,

Aplogise my english, im a spanish speaker.

Ive got a dataGrid where one their columns is a bit type. I would like where
one of this columns was true, the background color change to red color.

I tried to do this, but no way...

------------------------------------------------------------------------------------------------------



Create the event
this.dtgSortAndPaging.ItemDataBound += new
DataGridItemEventHandler(dtgSortAndPaging_ItemDataBound);


Create the function:
private void dtgSortAndPaging_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{
How I can work with de background propertie of de datagrid and change his
color??
}


Thanks!
 
K

Ken Cox [Microsoft MVP]

Hi Javier,

Here's an example in VB.NET that might give you the idea.

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
Dim drow As DataGridItem
Dim lbl As Label
drow = e.Item
lbl = CType(drow.Controls(0).Controls(1), Label)
If lbl.Text = "True" Then
e.Item.BackColor = Color.Beige
Else
e.Item.BackColor = Color.Red
End If
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Boolean") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
 
E

Elton Wang

Hi Javier,

Following code sample shows how to change a cell back
color according to its value:

ListItemType itemType = e.Item.ItemType;

if (itemType != ListItemType.Header && itemType !=
ListItemType.Pager && itemType != ListItemType.Footer &&
itemType != ListItemType.Seperator){

DataRowView drv = (DataRowView)e.Item.DataItem;
bool bData = (bool)drv[colIndex];
TableCell cell = e.Item.Cells[colIndex];

if (bData){
cell.BackColor = Color.Red;
}
}

By the way, it's better to code above logic in ItemCreated.

HTH

Elton Wang
(e-mail address removed)
 
J

Javier Pérez

Its run perfectly!!!!

Thanks very much!!!!


Elton Wang said:
Hi Javier,

Following code sample shows how to change a cell back
color according to its value:

ListItemType itemType = e.Item.ItemType;

if (itemType != ListItemType.Header && itemType !=
ListItemType.Pager && itemType != ListItemType.Footer &&
itemType != ListItemType.Seperator){

DataRowView drv = (DataRowView)e.Item.DataItem;
bool bData = (bool)drv[colIndex];
TableCell cell = e.Item.Cells[colIndex];

if (bData){
cell.BackColor = Color.Red;
}
}

By the way, it's better to code above logic in ItemCreated.

HTH

Elton Wang
(e-mail address removed)

-----Original Message-----
Hello All,

Aplogise my english, im a spanish speaker.

Ive got a dataGrid where one their columns is a bit type. I would like where
one of this columns was true, the background color change to red color.

I tried to do this, but no way...

---------------------------------------------------------- --------------------------------------------



Create the event
this.dtgSortAndPaging.ItemDataBound += new
DataGridItemEventHandler (dtgSortAndPaging_ItemDataBound);


Create the function:
private void dtgSortAndPaging_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{
How I can work with de background propertie of de datagrid and change his
color??
}


Thanks!



.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top