如何比较下拉列表值和 GridView 列值,如果它们不同,则隐藏

本文关键字:如果 隐藏 列值 GridView 何比较 比较 下拉列表 | 更新日期: 2023-09-27 17:55:44

如何比较DropDownList值和GridView列值,如果它们不同,请隐藏?

我有 5 列。第一个和第二个是 DropdownList 中的值,其他是某些 DMX 查询的结果。如果它们与用户在 DropDownList 中选择的值不同,我应该隐藏第三列中的值

我尝试了许多解决方案,但它们不起作用。最后,我尝试了这个功能。提前谢谢你!

private void GenerateUniqueData() {

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        DataControlFieldCell cell = GridView1.Rows[i].Cells[2] as DataControlFieldCell;
        if (cell.Text!=DropDownList6.SelectedItem.Value)
            GridView1.Rows[i].Visible = false;
    }
} 

如何比较下拉列表值和 GridView 列值,如果它们不同,则隐藏

看看这篇文章。这里要介绍的太多了:http://www.codeproject.com/Articles/43727/Show-Hide-GridView-Columns-in-ASP-NET

编辑:让我们用隐藏的代码在普通 ASP.NET 中尝试一下:首先要检查的是,您是否在下拉列表上打开了自动回发?如果没有到服务器的往返,您的代码没有任何区别,它不会被执行。

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>

这是一个工作示例....HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridTest.aspx.cs" Inherits="DataGridTest" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class DataGridTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> ddData = new List<string>();
            ddData.Add("Col 21");
            ddData.Add("Col 22");
            ddData.Add("Col 23");
            this.DropDownList1.DataSource = ddData;
            this.DropDownList1.DataBind();
            DataTable gridData = new DataTable();
            gridData.Columns.Add("Description");
            gridData.Columns.Add("Column 1");
            gridData.Columns.Add("Column 2");
            gridData.Columns.Add("Column 3");
            gridData.Rows.Add("Row 1", "Col 11", "Col 21", "Col 31");
            gridData.Rows.Add("Row 2", "Col 12", "Col 22", "Col 32");
            gridData.Rows.Add("Row 3", "Col 13", "Col 23", "Col 33");
            gridData.Rows.Add("Row 3", "Col 14", "Col 22", "Col 34");
            this.GridView1.DataSource = gridData;
            this.GridView1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            this.GridView1.Rows[i].Visible = true;
            string cellvalue = GridView1.Rows[i].Cells[2].Text;
            if (cellvalue.TrimEnd() != this.DropDownList1.SelectedItem.Value.TrimEnd())
                GridView1.Rows[i].Visible = false;
        }
    }
}