【问题标题】:Add Dropdown selected item text to specific column in gridview将下拉所选项目文本添加到gridview中的特定列
【发布时间】:2015-10-15 19:29:37
【问题描述】:

我有一个网格视图,它显示的结果集如下

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作为jan,在网格结果中它应该显示为

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

也分别与季度相同

我只需要将选定的下拉文本添加到网格视图标题列

注意:这里我的网格列属性是 autogeneratedcolumn = true

请帮我解决我们是否可以将所选文本添加到 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);
}

如果可能的话,请给我任何其他解决方案

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>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    请点击此链接,希望对您有所帮助。

    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;
    
            }
        }
    }
    }
    

    模态类:

    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 可以正常工作。

    【讨论】:

    • 感谢您的回复,我会通过它并让您知道。
    • 我在您提供的链接中没有找到任何有用的东西
    • 如果你想在运行时更改列文本,那么你必须遵循这个。根据你的问题。就我而言,它工作正常。
    • 我不想更改标题列的全文,我只需要添加(连接)选定的文本,还要记住我的网格有自动生成的列
    • 好的,请显示相关gridview的HTML。这样我就可以最终创建相同的场景。
    【解决方案2】:

    假设您的下拉列表如下所示:-

    <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;
            }
        }
    }
    

    同样适用于 Quarter 下拉菜单。如果您必须大量更改标题文本,请考虑使用StringBuilder

    更新:

    改用for 循环。我从1 开始循环,因为我想跳过第一列。因此,您可以操纵循环。

    【讨论】:

    • 我是否将列作为自动生成的列可能会增加超过 10 这是否适用于您的代码?
    • 有时我的列可能会减少,有时我的列可能会增加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多