timz_123
timz_123

Reputation: 437

DataView.RowFilter in DataTable does not return filtered records

I have a DataTable dt, in which 1200 records are coming. Below is the code:

DataTable dt = new DataTable();
dt = GetAccCode(c);

Now, using DataView I want to filter the record of dt, based on 1 column. Below is the code:

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(); 
    }

Full Code:
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 Code:

    <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>

But everytime dt returns all 1200 records not the filtered record.

Requirement: When I click on RadComboBox, it goes into _ItemRequested event. I want that when I search any record in RadComboBox, and if the record is there in DataTable then it shall goto GetStatusMessage ELSE part, or if the searched record is not there in DataTable then it shall goto GetStatusMessage IF part.
To get the searched record of RadComboBox from DataTable, I am using DataView.RowFilter method but everytime when I type any text in RadCombo, it never return the searched value and always show DataView.Count =0

Please reply how to solve this issue. I am stuck from 2 days in it.

Upvotes: 0

Views: 20055

Answers (3)

Mohit S
Mohit S

Reputation: 14024

I believe this might solve your problem.

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(); 
}

Just for testing purposes you can also hard code the values and see if it changes the result. for example:

dv.RowFilter = "AccountDescription LIKE '%mo%'" ;

and omit or comment the line

//dv.RowFilter = "Isnull(AccountDescription,'') <> ''";

to see what results you get.

Upvotes: 7

IamP
IamP

Reputation: 126

Try changing RowState

dv.RowStateFilter = DataViewRowState.CurrentRows

Upvotes: 1

timz_123
timz_123

Reputation: 437

I was using below code before but it was not working. Now it suddenly started working:

        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();
        }

Problem resolved. Thank you for the help @Mohit Shrivastava

Upvotes: 4

Related Questions