How to set first column's property of header row?

G

Guest

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.DataItem, "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.
 
L

Lenard Gunda

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
 
G

Guest

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.
 
L

Lenard Gunda

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

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.



:

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
 
G

Guest

Thanks.
It can work.


Lenard Gunda said:
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

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.



:

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.DataItem, "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.
 

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

Top