How can we set the background color in datatable through code-behind?
I am binding my data in a datatable and then bind that datatable into a datagrid. I want that some rows of the datatable should be hig开发者_StackOverflowhlighted with some color. How can we do that from code-behind?
You need to hook into the ItemDataBound event of the DataGrid, in order to dynamically set the properties you desire.
On your HTML, set the OnItemDataBound attribue of the DataGrid to a server-side event.
<asp:DataGrid id="ItemsGrid" runat="server"
...
OnItemDataBound="Item_Bound">
Then wire-up the server-side event handler:
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// some code to handle each bound item
}
You could then flip the colors based on the ListItemType enumeration:
if(e.Item.ItemType == ListItemType.Item)
e.Item.BackColor = Color.Red;
else if (e.Item.ItemType == ListItemType.AlternatingItem)
e.Item.BackColor = Color.Blue
You could use the following code snippet to color the rows of the grid:
foreach (DataGridItem item in DataGrid1.Rows) {
item.BackColor = Color.Red;
}
You can now place a condition in the foreach
loop to just color the rows you want:
counter = 0;
foreach (DataGridItem item in DataGrid1.Rows) {
if (counter % 2 == 0)
item.BackColor = Color.Black;
else
item.BackColor = Color.Red;
++counter;
}
Or, as mentioned in RPM1984's answer, you could also attach yourself to the OnItemDataBound
event, which is quite the better solution (depending on the situation), since you won't have to iterate through all the rows by hand:
ASPX:
<asp:DataGrid ID="DataGrid1" OnItemDataBound="ItemDataBound" runat="server">
Code-behind:
public void ItemDataBound(object sender, DataGridItemEventArgs e) {
e.Item.BackColor = Color.Red;
}
Regarding the coloring of the invoices and receipts, you could use the following code snippet to achieve what you want, assuming that you're adapting the approach of using the OnItemDataBound
event to update the row's BackColor
:
Code-behind:
public void ItemDataBound(object sender, DataGridItemEventArgs e) {
SalesPosition salesItem = e.Item.DataItem as SalesPosition;
if(salesItem == null) return;
if(salesItem.Type == SalesPositionType.Invoice)
e.Item.BackColor = Color.Yellow;
else if(salesItem.Type == SalesPositionType.Receipt)
e.Item.BackColor = Color.Green;
}
You have to add in your Tag ASP an event OnDatabound:
You need to TAG the event Datarow binding with this
<asp:GridView ID="gvAps" BackColor="Yellow" runat="server" HeaderStyle-CssClass="GridView_Entete" OnRowDataBound="RowDataBound" />
and in the code-behind specify action for the event:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (drv["INVOICES/RECEIPTS "].ToString()) == "INVOICE" )
{
e.Row.Attributes.Remove("Class");
e.Row.Attributes.Add("Class", "GridView_New");
}
}
You can specify in the code a css class or simply modify the color of the row..
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (drv["INVOICES/RECEIPTS "].ToString()) == "INVOICE" )
}
e.Item.BackColor = Color.Green;
}
}
精彩评论