0

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.

6
  • did you tried dv.RowStateFilter = DataViewRowState.ModifiedCurrent; after applying RowFilter
    – Mohit S
    Aug 12, 2015 at 2:14
  • @MohitShrivastava: Thank you for the reply. But when I add your suggested line in my code (please check my posted question again , I had modified it) everytime dv.count = 0 , And still I do not get filtered data when I do filtering. Please reply
    – timz_123
    Aug 12, 2015 at 2:23
  • try this dv.RowFilter = "Isnull(AccountDescription,'NULL') <> 'NULL'";
    – Mohit S
    Aug 12, 2015 at 3:17
  • and just to make sure it must be filtering records please try "AccountDescription LIKE %'" + e.Text + "'%";
    – Mohit S
    Aug 12, 2015 at 3:25
  • @MohitShrivastava: Please check my edited code. When I try to use dv.RowStateFilter = DataViewRowState.ModifiedCurrent; line, everytime dv.count=0 and when I use current edited code, everytime dv.count=<total records of dt>i.e.,1200 Please suggest what shall I change to get the desired result.
    – timz_123
    Aug 12, 2015 at 3:33

3 Answers 3

7

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.

8
  • 1
    Dont know what is wrong going on. Everytime I get dv.count=0 for filtered records. Also, dv.RowFilter = "AccountDescription LIKE %'" + e.Text + "'%" ; returns all the records 1200 in place of only filter records. I am stuck in this issue from 2 days..Unable to understand the problem. Please help
    – timz_123
    Aug 12, 2015 at 4:00
  • Thank you so much for the help. Please check my posted question again, there I added the full code (under Full Code section) along with Requirement description. If any confusion in my requirement please let me know. Waiting for your reply
    – timz_123
    Aug 12, 2015 at 4:25
  • 1 more thing: When I hard code the e.Text in this line: string searchParamText = e.Text; it gives me expected output. But when I directly use this line it never give expected out put. This is the only problem im facing. If this is resolved my requirement will be done. Please reply
    – timz_123
    Aug 12, 2015 at 4:33
  • Friend then the problem is not with the code but with the input you are using. What is the value of e.Text when you debug.
    – Mohit S
    Aug 12, 2015 at 4:46
  • e.Text value is whatever I type in RadComboBox. When I use string searchParamText = e.Text; it returns all the rows of DataTable in DataView.RowCount and when I hard code e.Text value it gives expected output. Please tel what is wrong in my code.
    – timz_123
    Aug 12, 2015 at 5:16
4

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

0
1

Try changing RowState

dv.RowStateFilter = DataViewRowState.CurrentRows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.