一个字符串如下,人名用半角逗号分开:张三,李四,王五,赵六一个GridView控件绑定一个表,表中有一列数据(姓名),可能的数据如下:张三,李四,牛七,王五,马八
现在想要的结果:凡是字符串中存在的人名在GridView中显示,不存在则不显示,如何做呢?谢谢
现在想要的结果:凡是字符串中存在的人名在GridView中显示,不存在则不显示,如何做呢?谢谢
解决方案 »
- google map api
- Image服务器控件无法接受另一个页面传过来的值
- 哪位有Asp.net3.5揭秘([asp.net.3.5揭秘].ASP.NET.3.5.Unleashed)
- 表与表之间有联系必须要建主外键关系吗?
- 关于Socket编程的问题,帮帮忙呀各位大虾
- 请教关于网页控件使用的问题, 高手指点, 受益散分!!
- 菜鸟的问题:在VS2003下无法将下面的CS文件编译成DLL文件,在线急等!!!
- 散分了,快来看看!缓存的问题
- 热帖
- 如何关闭executereader,我这没有帮助文件,请帮忙,很简单
- asp.netmtextbox的内容如何提交到服务器上的一个表里
- 树形结构目录是怎样做出来的?
<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;
}
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();
凡是字符串中存在的人名的那行在GridView中显示,不存在那行则不显示,如何做呢?
比如
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();
}
if(s.IndexOf(dt.Rows[i]["name"].ToString())!=-1) 改成if(s.IndexOf(dt.Rows[i]["name"].ToString())==-1)
<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;
}
}
}