Click here to Skip to main content
13,140,231 members (47,968 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a gridview which consists of columns with different datatypes..
I have provided type and search functionality...
Whenever user types his criteria values of all columns are checked and appropriate rows are filtered..
It works fine with varchar... But has some problems with int,float...
I am using dv.RowFilter....

criteria = ViewState["Criteria"].ToString();
                string selected = dropdownCriteria.SelectedValue;
                string textEntered = searchBox.Value.ToString();
                DataSet ds = new DataSet();
                ds = (DataSet)ViewState["InitialData"];
                DataView dv = new DataView();
                dv = ds.Tables[0].DefaultView;
                dv.RowFilter = selected + " like '" + textEntered + "%'";
                reportGrid.DataSource = dv;
                reportGrid.DataBind();
                reportGrid.AllowPaging = false;
                reportGrid.AllowSorting = false;
                reportGrid.AutoGenerateDeleteButton = false;
                reportGrid.AutoGenerateEditButton = false;
                panelGrid.Update();
 
                DataTable dt = new DataTable();
                dt = dv.ToTable();
                DataSet tempDS = new DataSet();
                tempDS.Tables.Add(dt);
 
                reportGrid.AllowPaging = true;
                reportGrid.AllowSorting = true;
                reportGrid.AutoGenerateDeleteButton = true;
                reportGrid.AutoGenerateEditButton = true;
                ViewState["CurrData"] = tempDS;
Posted 27-Feb-13 6:14am
Updated 28-Feb-13 16:40pm
v3
Comments
richcb 27-Feb-13 12:16pm
   
You will have to post the code you are using. We cannot see what is going on without it.
Member 9644631 27-Feb-13 12:21pm
   
criteria = ViewState["Criteria"].ToString();
string selected = dropdownCriteria.SelectedValue;
string textEntered = searchBox.Value.ToString();
DataSet ds = new DataSet();
ds = (DataSet)ViewState["InitialData"];
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.RowFilter = selected + " like '" + textEntered + "%'";
reportGrid.DataSource = dv;
reportGrid.DataBind();
reportGrid.AllowPaging = false;
reportGrid.AllowSorting = false;
reportGrid.AutoGenerateDeleteButton = false;
reportGrid.AutoGenerateEditButton = false;
panelGrid.Update();

DataTable dt = new DataTable();
dt = dv.ToTable();
DataSet tempDS = new DataSet();
tempDS.Tables.Add(dt);

reportGrid.AllowPaging = true;
reportGrid.AllowSorting = true;
reportGrid.AutoGenerateDeleteButton = true;
reportGrid.AutoGenerateEditButton = true;
ViewState["CurrData"] = tempDS;
richcb 27-Feb-13 12:26pm
   
It looks like to me, when a number is entered you are getting the ToString() of them and that is probably what the problem is. If you can use a Switch Case and determine if the entered data is text or numeric, you could then convert the data to which ever datatype you need. Does that make sense?
Member 9644631 27-Feb-13 22:56pm
   
Ya that could be help... But it would be more helpful if i consider only those columns with same data types with one entered by user.. That is if user enters a number then consider only those columns with data type as float or int
richcb 28-Feb-13 9:47am
   
Ok, what about doing something like this: object info = textEntered;
info.GetType();

This will get the type of the textEntered variable and then you could determine what you need to do with it.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Check this out in Lambda Expression:
criteria = ViewState["Criteria"].ToString();
string selected = dropdownCriteria.SelectedValue;
string textEntered = searchBox.Value.ToString();
DataSet ds = new DataSet();
ds = (DataSet)ViewState["InitialData"];
reportGrid.DataSource = ds.Tables[0].AsEnumerable().Where(s=>s[selected].ToString().StartsWith(textEntered)).OrderBy(s=>s[selected].ToString()).CopyToDataTable();
reportGrid.DataBind();
reportGrid.AllowPaging = false;
reportGrid.AllowSorting = false;
reportGrid.AutoGenerateDeleteButton = false;
reportGrid.AutoGenerateEditButton = false;
panelGrid.Update();
 
DataTable dt = new DataTable();
dt = ds.Tables[0].AsEnumerable().Where(s=>s[selected].ToString().StartsWith(textEntered)).OrderBy(s=>s[selected].ToString()).CopyToDataTable();
DataSet tempDS = new DataSet();
tempDS.Tables.Add(dt);
 
reportGrid.AllowPaging = true;
reportGrid.AllowSorting = true;
reportGrid.AutoGenerateDeleteButton = true;
reportGrid.AutoGenerateEditButton = true;
ViewState["CurrData"] = tempDS;


--Amit
  Permalink  
v2
Comments
Member 9644631 28-Feb-13 23:08pm
   
Will u please explain this a bit.. But this does not seem to be working with int and float...
_Amy 28-Feb-13 23:12pm
   
Here I've used lambda expression to filter the data form your datatable and rebind it to the grid. Since, you was using Like '%' keyword, I replaced it with StartWith() function. Both are similar.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Do something like this

string search = String.Format("Convert(<column name="">,'System.String') LIKE '{0}*'", EscapeLikeValue(Search.Trim()));

DataView dv = new DataView(dt);

dv.RowFilter = search;

dt = dv.ToTable();

-------------------------------------------------------------------------------------------

public string EscapeLikeValue(string valueWithoutWildcards)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < valueWithoutWildcards.Length; i++)
            {
                char c = valueWithoutWildcards[i];
                if (c == '*' || c == '%' || c == '[' || c == ']')
                    sb.Append("[").Append(c).Append("]");
                else if (c == '\'')
                    sb.Append("''");
                else
                    sb.Append(c);
            }
            return sb.ToString();
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

have a look at this for complete referece of filter

http://www.csharp-examples.net/dataview-rowfilter/[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 26 Nov 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100