一个字符串如下,人名用半角逗号分开:张三,李四,王五,赵六一个GridView控件绑定一个表,表中有一列数据(姓名),可能的数据如下:张三,李四,牛七,王五,马八
现在想要的结果:凡是字符串中存在的人名在GridView中显示,不存在则不显示,如何做呢?谢谢

解决方案 »

  1.   

    可以在前台绑定方法,后台处理大致如下:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# ShowName(Eval("yourField")) %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>//后台大意如此,未调试,考虑性能可用StringBuilder类
        protected string ShowName(object o)
        {
            string str = "张三,李四,王五,赵六";
            string [] existNames = str.Split(',');
            string result = string.Empty;
            if (o == null || o == DBNull.Value)
                return string.Empty;        string[] names = o.ToString().Split(',');        foreach (string name in names)
            {
                foreach (string existName in existNames)
                {
                    if (name == existName)
                    {
                        result += name;
                        break;
                    }
                }
            }
            return result;
        }
      

  2.   


                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Name", typeof(string)));
                dt.Rows.Add("张三");
                dt.Rows.Add("李四");
                dt.Rows.Add("牛七");
                dt.Rows.Add("王五");
                dt.Rows.Add("马八");
                string str = "张三,李四,王五,赵六";
                string[] temp = str.Split(',');
                for (int i = 0; i < temp.Length; i++)
                    temp[i] = "'" + temp[i] + "'";
                DataRow[] rows = dt.Select("Name in (" + string.Join(",", temp) + ")");
                DataTable dtNew = new DataTable();
                foreach (DataColumn dc in dt.Columns)
                    dtNew.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
                foreach (DataRow dr in rows)
                    dtNew.ImportRow(dr);
                GridView1.DataSource = dtNew;
                GridView1.DataBind();
      

  3.   

    非常对不起,是我的问题表答的不对,想要的结果:
    凡是字符串中存在的人名的那行在GridView中显示,不存在那行则不显示,如何做呢? 
      

  4.   

    遍历datatable,比较如果姓名在字符串中存在就把这行删了
    比如
    string s="张三,李四,王五";
    DataTable dt=getinfo();
    for(int i=0;i<dt.Rows.count;i++)
    {
    if(s.IndexOf(dt.Rows[i]["name"].ToString())!=-1)
    {
    dt.Rows[i].Delete();
    }
    dt.AcceptChange();
    GridView1.DataSouce=dt;
    GridView1.DataBind();
    }
      

  5.   

    额打错了,只显示字符串里面存在的人应该把判断改成不存在
    if(s.IndexOf(dt.Rows[i]["name"].ToString())!=-1) 改成if(s.IndexOf(dt.Rows[i]["name"].ToString())==-1) 
      

  6.   

    如果我做可能会在GridView.RowDataBound事件里,用当前行的数据与字符串比较,然后根据判断结果用当前行(GridViewRow)的Visible属性=false,或者指定当前行的CssClass为display:none的css类。至于与字符串比较那里可能会试着用LINQ。具体的我就不试了。
      

  7.   

    不知道你有没有试试我3楼的方法,以下方法也是可以的,但不是最好的方法,比如你PageSize为10,如果只有三行数据在字符串中出现,那这一页就只能显示三行了,所以我觉得最好还是从数据源上下手        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%#  Eval("Name").ToString() %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string str = "张三,李四,王五,赵六";
                    string[] temp = str.Split(',');
                    Label lab = e.Row.FindControl("Label1") as Label;
                    if (lab != null && Array.IndexOf(temp, lab.Text) < 0)
                    {
                        e.Row.Visible = false;
                    }
                }
            }