Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
4.06/5 (7 votes)
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
Updated 28-Feb-13 16:40pm
v3
Comments
Richard C Bishop 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;
Richard C Bishop 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
Richard C Bishop 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.

Check this out in Lambda Expression:
C#
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
 
Share this answer
 
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.
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();

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

C#
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();
        }
 
Share this answer
 
have a look at this for complete referece of filter

http://www.csharp-examples.net/dataview-rowfilter/[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900