Created
March 12, 2015 18:32
-
-
Save Ryanb58/12f6a72d19dbfa0c86b0 to your computer and use it in GitHub Desktop.
Simple function that builds a row filter query via the parameters in the querystring. Checks to ensure that the key matches a column in the dataviewer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void getSearchQueryFromQueryString() | |
{ | |
try | |
{ | |
//String for row query. | |
StringBuilder sb = new StringBuilder(); | |
//Get all the information in the query strings. | |
NameValueCollection querystrings = Request.QueryString; | |
//Is there is any parameters provided? | |
if (querystrings.HasKeys()) | |
{ | |
//Loop through each. | |
foreach(string key in querystrings.Keys) | |
{ | |
string value = querystrings[key]; | |
bool columnExistInData = false; | |
//Does this key match a column in the data viewer? | |
foreach (DataColumn dc in dv.Table.Columns) | |
{ | |
if (dc.ColumnName.ToString().Trim().ToLower() == key.Trim().ToLower()) | |
{ | |
//Match! | |
columnExistInData = true; | |
break; //Get out of the loop. | |
} | |
} | |
//Make sure both are clean and exist. | |
if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value) && columnExistInData) | |
{ | |
//If they both have value to them. Check if the value is a string or int. | |
int result; | |
bool isInt = int.TryParse(value, out result); | |
//Check if we need a transition word. | |
if (sb.Length > 0) | |
{ | |
sb.Append(" AND "); | |
} | |
//Is it an integer or string? | |
if (isInt) | |
{ | |
//Value is a INT. | |
sb.Append("[" + key + "] = " + value); | |
} | |
else | |
{ | |
//Value is a string. | |
sb.Append("[" + key + "] like '*" + value + "*'"); | |
} | |
} | |
} | |
//set the filter. Make dv.RowFilter = filterQuery later on in code after calling this function. | |
filterQuery = sb.ToString(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
//Something went wrong. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment