2015-10-15 22 views
0

我具有被顯示的結果如下設定添加下拉所選項目的文本以特定列中的GridView

customer |2011 shipped qty|2011 sales price|2012 shipped qty|2012 sales price 

    aa  1      2.00    2     5.50 
    cc  2      3.00    4     6.25 

網格視圖,我具有兩個下拉列表作爲
monthdropdown1和quarterdropdown2

如果用戶爲選擇揚monthdropdown1,在網格中導致它應該顯示作爲

customer|2011 shipped qty|2011 sales price| 2012 shipped qty| 2012 sales price| 
      jan     jan     jan   jan 

    aa  1      2.00    2     5.50 
    cc  2      3.00    4     6.25 

和也相同季度分別

我只需要選擇dropdowntext添加到網格視圖標題列

注:這裏我格列屬性是autogeneratedcolumn =真

請找我的解決方案,我們是否可以添加選定的文本到GridView的列標題,也可以選定文本

我曾嘗試與此代碼在Rowdatabound event,並沒有爲我工作

if (e.Row.RowType == DataControlRowType.Header) 
{ 
    GridViewRow HeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); 
    TableCell HeaderCell2 = new TableCell(); 
    HeaderCell2.Text = (DropDownList1.SelectedItem.Text); 
    HeaderCell2.ColumnSpan = 0; 
    HeaderRow.Cells.Add(HeaderCell2); 
    DataGrid1.Controls[0].Controls.AddAt(0, HeaderRow); 
} 
01添加saperate標題列

請找我任何其他解決方案,如果可能的

HTML

<asp:GridView ID="DataGrid1" Style="visibility: visible" runat="server" AlternatingRowStyle-BackColor="#E9EDF5" 
       Font-Names="Arial" ForeColor="#09538A" Font-Size="12px" BackColor="#ffffff" BorderColor="DarkGray" 
       Font-Bold="true" HeaderStyle-BackColor="#298DC7" EnableViewState="false" CellSpacing="20" 
       CellPadding="10" ShowFooter="false" HeaderStyle-Font-Bold="true" AutoGenerateColumns="True" OnRowDataBound="DataGrid1__RowDataBound"> 
        <RowStyle HorizontalAlign="Right" Height="20px"/> 
        <alternatingrowstyle Height="20px" BackColor="#E9EDF5"/> 
<%--    OnRowCommand="DataGrid1__RowCommand" OnRowDataBound="DataGrid1__RowDataBound">--%> 
       <HeaderStyle Font-Names="Arial;" CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="White" 
        Font-Bold="True" Height="20" BackColor="#298DC7"></HeaderStyle> 
    <AlternatingRowStyle BackColor="#E9EDF5" /> 
      </asp:GridView> 

回答

0

請點擊此鏈接,希望這會幫助你。

On Button click change header text

如果您仍然面臨着問題,那麼請讓我知道我會爲你創建一個樣本。

在您的代碼:

HTML:

<form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="DataGrid1" Style="visibility: visible" runat="server" AlternatingRowStyle-BackColor="#E9EDF5" 
      Font-Names="Arial" ForeColor="#09538A" Font-Size="12px" BackColor="#ffffff" BorderColor="DarkGray" 
      Font-Bold="true" HeaderStyle-BackColor="#298DC7" EnableViewState="false" CellSpacing="20" 
      CellPadding="10" ShowFooter="false" HeaderStyle-Font-Bold="true" AutoGenerateColumns="True"> 
      <RowStyle HorizontalAlign="Right" Height="20px" /> 
      <AlternatingRowStyle Height="20px" BackColor="#E9EDF5" /> 
      <%--    OnRowCommand="DataGrid1__RowCommand" OnRowDataBound="DataGrid1__RowDataBound">--%> 
      <HeaderStyle Font-Names="Arial;" CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="White" 
       Font-Bold="True" Height="20" BackColor="#298DC7"></HeaderStyle> 
      <AlternatingRowStyle BackColor="#E9EDF5" /> 
     </asp:GridView> 
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Text="Jan" Value="0"></asp:ListItem> 
      <asp:ListItem Text="Feb" Value="1"></asp:ListItem> 
      <asp:ListItem Text="Mar" Value="2"></asp:ListItem> 
     </asp:DropDownList> 

     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> 
      <asp:ListItem Text="Quater1" Value="0"></asp:ListItem> 
      <asp:ListItem Text="Quater2" Value="1"></asp:ListItem> 
      <asp:ListItem Text="Quater3" Value="2"></asp:ListItem> 
     </asp:DropDownList> 

    </div> 

</form> 

.CS:

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    List<employee> obj = new List<employee>() { 
      new employee(1, "Sunny1"), 
      new employee(2, "Sunny2"), 
      new employee(3, "Sunny3"), 
      new employee(4, "Sunny4"), 
      new employee(5, "Sunny5"), 
      new employee(6, "Sunny6"), 
      new employee(7, "Anny7")}; 
    DataGrid1.DataSource = obj; 
    DataGrid1.DataBind(); 
} 
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    Changetext(); 
} 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 

    Changetext(); 
} 

private void Changetext() 
{ 
    string str = DropDownList1.SelectedItem.ToString() + " "; 
    str += DropDownList2.SelectedItem.ToString(); 
    if (DataGrid1.Rows.Count > 0) 
    { 
     for (int i = 0; i < DataGrid1.HeaderRow.Cells.Count; i++) 
     { 
      DataGrid1.HeaderRow.Cells[i].Text = DataGrid1.HeaderRow.Cells[i].Text + " " + str; //selectedvalue/text; 

     } 
    } 
} 
} 

MODAL類:

public class employee 
{ 
public int ID { get; set; } 
public string Name { get; set; } 
public DateTime Date { get; set; } 

public employee(int id, string _name) 
{ 
    ID = id; 
    Name = _name; 
    Date = DateTime.Now; 
} 

public employee() 
{ 

} 

} 

使用此代碼,讓我知道。與您的HTML這是工作正常。

+0

感謝您的答覆,我會通過它,讓你知道。 – sowmya

+0

我在您提供的鏈接中找不到任何有用的東西 – sowmya

+0

如果您想在運行時更改列文本,那麼您必須遵循這一點。根據你的問題。在我的結尾它工作正常。 – Sunny

0

假設你的下拉列表看起來是這樣的: -

<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" 
    OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged" > 
    <asp:ListItem Selected="True" Text="Select" Value="-1"></asp:ListItem> 
    <asp:ListItem Text="January" Value="1"></asp:ListItem> 
    <asp:ListItem Text="February" Value="2"></asp:ListItem> 
</asp:DropDownList> 

你可以操縱這樣的GridView的標題: -

protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if(ddlMonth.SelectedValue != "-1") 
    { 
     for (int i = 1; i < DataGrid1.HeaderRow.Cells.Count; i++) 
     { 
      DataGrid1.HeaderRow.Cells[i].Text += " " + ddlMonth.SelectedItem.Text; 
     } 
    } 
} 

爲相若方式下拉季度。如果您必須更改標題文本,請考慮使用StringBuilder

更新:

使用for環代替。我已經從1開始循環,因爲我想跳過第一列。因此你可以操縱循環。

+0

是我有列作爲autogeneraated列可能增加超過10將這個工作與您的代碼? – sowmya

+0

有些時候我的列可能會減少,有時我的列可能會增加 – sowmya

+0

感謝您的解決方案 – sowmya

相關問題