Here is a code for an item data bound event handler, I think this should
work :)
private void DataGrid1_ItemDataBound(
object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
// check that item is header
if ( e.Item.ItemType == ListItemType.Header )
{
// check that we have cells
if ( e.Item.Cells.Count > 0 )
{
e.Item.Cells[0].BackColor = Color.Green;
}
}
}
This should run when data is bound to the grid. If checks if the item is
a header item, and if it is, checks if there are any cells. If then
changes the backcolor of that cell. You can also access almost anything
in the DataGrid in a similar way, after it has been built (data bound).
Columns count can be 0, if you use automatic column creation. In that
case, the columns are automatically created for you, but are never added
to the Columns collection itself.
-Lenard
Grace wrote:
I write as following:
DataGrid dg=(DataGrid)obj;
dg.Columns[0].HeaderStyle.BackColor=Color.Green;
but it shows error message.
I check dg.Columns.Count, it is always 0.
Could you write some codes to teach me how to solve it?
Thanks.
"Lenard Gunda" wrote:
Hi,
You could add an ItemDataBound event to the DataGrid. Then, in the event
handler, check when you get a header type of an item. Get the first
column, and change the color programatically.
-Lenard
Grace wrote:
I use a datagrid included in a repeater.
as following: (.aspx)
<asp:repeater id="rp_perform" Runat="server">
<HeaderTemplate>
<table border="0" bgcolor="gray" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="white">
<td width="100%">
<asp:datagrid id="dg_perform" Runat="server" DataSource='<%#
GetDataTable((int)DataBinder.Eval(Container.Dat aItem, "Product_No")) %>'
AutoGenerateColumns="True" GridLines="Both" BorderColor="DimGray"
BorderWidth="2" CellPadding="3" CellSpacing="0">
<HeaderStyle BackColor="#99ccff" Font-Bold="True" />
<ItemStyle BackColor="#FFFFCC" />
</asp:datagrid>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td height="25" bgcolor="white"></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
I want to change first column's backcolor of header row in each datagrid.
I catch every datagrid, it can set item's settings, but can't set first
column's settings of header row.
How to do it?
Thanks.