【发布时间】:2011-12-23 16:17:32
【问题描述】:
我必须如何在 Dataview 中设置 RowFilter,我从行中接收所有值(在我的情况下是 iSBN),它们 类似于 搜索字符串(在我的情况下是 isbn) 我尝试了以下代码,但没有成功。 我认为它必须是 SQL 语法,但可能我错了。
此代码有效。我也很期待。
DataView custView = new DataView(_dset.Tables["Book"], "ISBN like '%isbn%'", "ISBN", DataViewRowState.CurrentRows);
DataView custView = new DataView(_dset.Tables["Book"], "", "ISBN", DataViewRowState.CurrentRows);
_lBdatenOutput.Items.Clear();
foreach (DataRowView myDRV in custView)
{
DataRow dr = myDRV.Row;
if((dr["ISBN"].ToString().IndexOf(isbn) >= 0))
{
foreach (DataColumn cl in custView.Table.Columns)
{
_lBdatenOutput.Items.Add("Spalten-Name: " + " \t " + cl.ColumnName + " \t" + dr[cl]);
}
_lBdatenOutput.Items.Add(seperator1);
}
我必须在Dataview的构造函数中放入什么,我可以用下面的代码解决它?
DataView custView = new DataView(_dset.Tables["Book"], "ISBN like '%isbn%'", "ISBN", DataViewRowState.CurrentRows);
DataRowView[] foundRows = custView.FindRows(new object[] { isbn });
if (foundRows.Length == 0)
{
_lBdatenOutput.Items.Clear();
_lBdatenOutput.Items.Add("No matches found");
}
else
{
_lBdatenOutput.Items.Clear();
foreach (DataRowView myDRV in foundRows)
{
DataRow dr = myDRV.Row;
foreach (DataColumn cl in custView.Table.Columns)
{
_lBdatenOutput.Items.Add("Spalten-Name: " + " \t " + cl.ColumnName + " \t" + dr[cl]);
}
}
}
我的老板坐在我的右手边,试了 1 个小时,如果你能找到答案就太好了
感谢您的帮助
【问题讨论】:
-
如果我正确理解了您的代码,您想找到任何 ISBN 的一部分是 ISBN 的行吗?这将简单地返回所有行,因为每个 ISBN 都是其自身的一部分。此冗余过滤器的RowFilter 将是:
custView.RowFilter = "ISBN LIKE '%' + ISBN + '%'"。如果您有一个名为isbn的变量,并且您想查找所有匹配的记录,则以下表达式将起作用:"ISBN LIKE '%" + isbn + "%'"msdn.microsoft.com/en-us/library/… -
谢谢,就是这样。你为什么不发表你的评论作为答案?