我有一个DataTable dt ,其中有1200条记录。 以下是代码:
DataTable dt = new DataTable();
dt = GetAccCode(c);
现在,使用DataView我想基于1列过滤 dt 的记录。 以下是代码:
EDITED
DataView dv = new DataView(dt);
dv.RowFilter = "AccountDescription LIKE '" + e.Text + "' " ;
dv.RowFilter = "Isnull(AccountDescription,'NULL') <> 'NULL'";
//dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
if (dv.Count > 0)
{
dt = dv.ToTable();
}
完整代码:
C#CODE:
private const int ItemsPerRequest = 50;
private static string GetStatusMessage(int offset, int total)
{
if (total <= 0)
return "No matches";
return String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", offset, total);
}
public DataTable GetTable()
{
// columns.
DataTable table = new DataTable();
table.Columns.Add("AccountCodeID", typeof(Guid));
table.Columns.Add("AccountDescription", typeof(string));
// DataRows.
table.Rows.Add(Guid.NewGuid(), "9830.58578 Furniture&Food counter ");
table.Rows.Add(Guid.NewGuid(), "09.323232 New-release add ons");
table.Rows.Add(Guid.NewGuid(), "84.238799 G&D - Depot from Outerjoin");
table.Rows.Add(Guid.NewGuid(), "141.44445043-Random&Access-Memory hydero 048kkjug ");
table.Rows.Add(Guid.NewGuid(), "142.85223 Account's hyqure");
table.Rows.Add(Guid.NewGuid(), "324.989384934 F&D Admin-Dept Working ");
table.Rows.Add(Guid.NewGuid(), "77.234434 Unique's Question why not's so");
return table;
}
protected void ddlAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
string c = ddlCompany.SelectedValue.ToString();
DataTable dt = GetTable();
DataView dv = new DataView(dt);
string searchParamText = e.Text;
dv.RowFilter = string.Format("AccountDescription LIKE '%{0}%'", searchParamText);
//dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
int a = dv.Count;
if (dv.Count > 0)
{
dt = dv.ToTable();
}
RadComboBox combo = (RadComboBox)sender;
int itemOffset = e.NumberOfItems;
int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
e.EndOfItems = endOffset == dt.Rows.Count;
for (int i = itemOffset; i < endOffset; i++)
{
combo.Items.Add(new RadComboBoxItem(dt.Rows[i]["AccountDescription"].ToString(), dt.Rows[i]["AccountDescription"].ToString()));
}
e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
}
HTML代码:
<EditItemTemplate>
<asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false">
</asp:Label>
<telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310" HighlightTemplatedItems="true" CausesValidation="true" OnItemsRequested="ddlAccountCode_ItemsRequested" ItemsPerRequest="10" EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true" Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID">
</telerik:RadComboBox>
</EditItemTemplate>
但每次 dt 都会返回所有1200条记录,而不是过滤后的记录。
要求:当我点击RadComboBox时,它会进入_ItemRequested
事件。我希望在我搜索RadComboBox中的任何记录时,如果记录在DataTable中,那么它应该转到GetStatusMessage
ELSE部分,或者如果搜索到的记录不在DataTable中那么它应该转到GetStatusMessage
IF部分。
要从DataTable获取RadComboBox的搜索记录,我使用的是DataView.RowFilter方法,但每当我在RadCombo中键入任何文本时,它都不会返回搜索到的值并始终显示DataView.Count = 0
请回复如何解决此问题。我被困了2天。
答案 0 :(得分:5)
我相信这可能会解决您的问题。
DataTable dt = new DataTable();
dt = GetAccCode(c);
DataView dv = new DataView(dt);
dv.RowFilter = "AccountDescription LIKE '%" + e.Text + "%'" ;
dv.RowFilter = "Isnull(AccountDescription,'') <> ''";
dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
if (dv.Count > 0)
{
dt = dv.ToTable();
}
仅出于测试目的,您还可以对值进行硬编码,并查看它是否会更改结果。例如:
dv.RowFilter = "AccountDescription LIKE '%mo%'" ;
并省略或评论该行
//dv.RowFilter = "Isnull(AccountDescription,'') <> ''";
看看你得到了什么结果。
答案 1 :(得分:4)
之前我使用的是以下代码,但它无效。现在它突然开始工作了:
DataTable dt = GetAccCode(c);
DataView dv = new DataView(dt);
string txt = e.Text;
dv.RowFilter = string.Format("AccountDescription LIKE '%{0}%'", txt);
int a = dv.Count;
if (dv.Count > 0)
{
dt = dv.ToTable();
}
问题已解决。感谢您帮助@Mohit Shrivastava
答案 2 :(得分:1)
尝试更改RowState
dv.RowStateFilter = DataViewRowState.CurrentRows