2017-04-21 56 views
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 
      } 
     } 
    } 

回答

0

編輯:自DataGrid,其項目將必然是DataList控制,必須首先得到參考網格控制。你可以這樣:

//get grid as Item's parent 
DataGrid grid = e.Item.Parent.NamingContainer as DataGrid 
//alternatively, you can search for grid in DataList controls, like this: 
// yourList.FindControl("dgWarrant1") as DataGrid; 

// if INCOME_TREATMENT <> R 
if (e.Item.Cells[5].Text != "R") 
{ 
    grid.Columns[4].HeaderText = "DISTRIBUTION DATE"; 
} 
else 
{ 
    grid.Columns[4].HeaderText = "REINVESTMENT DATE"; 
} 
+0

嗨。感謝您的回覆。但dgWarrant1位於DataList中,因此無法從代碼隱藏中訪問。我會更新上面的html。你知道如何訪問dgWarrant1嗎? – Intan

+0

@Eera,我編輯了我的代碼。現在嘗試看看它是否可以幫助你。 – Nino

+0

Hi @Nino。這麼晚纔回復很抱歉。感謝您的幫助,我現在可以訪問網格。但是列標題標題沒有改變。請幫忙。 – Intan