0
我有一个数据网格想要根据单元格的值更改列的标题文本。如何获取单元格的标题并更改文本?C#ASP.NET如何根据项目值更改数据网格标题文本
请帮忙。提前致谢!
的Datagrid下面的html:
<asp:DataList ID="dlWarrant" runat="server" Width="100%" OnItemDataBound="dlWarrant_OnItemDataBound">
<ItemTemplate>
<asp:Table runat="server" ID="resulttable" Width="100%" >
<asp:TableRow>
<asp:TableCell ColumnSpan="6" HorizontalAlign="left">
<asp:DataGrid CellSpacing="1" runat="server" ID="dgWarrant1" CellPadding="4" ForeColor="#333333"
GridLines="Both" AutoGenerateColumns="False" HorizontalAlign="Center" Width="100%"
DataSource='<%# Eval("DETAILS1") %>' Font-Size="8.5pt" OnItemDataBound="dgWarrant1_OnItemDataBound">
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle HorizontalAlign="Center" Mode="NumericPages" CssClass="PagerStyle" BackColor="#284775" ForeColor="White" />
<Columns>
<asp:BoundColumn DataField="PA_NO" HeaderText="WARRANT NO." HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="TOTAL_UNITS_HELD" HeaderText="NO. OF UNITS HELD" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" DataFormatString="{0:#,##0.00}" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="YR_ENDED" HeaderText="YEAR ENDING" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="TYPE_OF_DISTRIBUT" HeaderText="TYPE OF DISTRIBUTION" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="TRANS_DATE" HeaderText="REINVESTMENT DATE" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="INCOME_TREATMENT" HeaderText="INCOME_TREATMENT" ItemStyle-CssClass="hideGridColumn" HeaderStyle-CssClass="hideGridColumn" />
</Columns>
</asp:DataGrid>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:DataList>
我的后台代码:
protected void dgWarrant1_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// if INCOME_TREATMENT <> R
if (e.Item.Cells[5].Text != "R")
{
//change the header of Cells[4] to DISTRIBUTION DATE
}
else
{
//change the header of Cells[4] to REINVESTMENT DATE
}
}
}
嗨。感谢您的回复。但dgWarrant1位于DataList中,因此无法从代码隐藏中访问。我会更新上面的html。你知道如何访问dgWarrant1吗? – Intan
@Eera,我编辑了我的代码。现在尝试看看它是否可以帮助你。 – Nino
Hi @Nino。这么晚才回复很抱歉。感谢您的帮助,我现在可以访问网格。但是列标题标题没有改变。请帮忙。 – Intan