Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 21, 2012 07:39 AM by Hua-Jun Li - MSFT
Member
35 Points
67 Posts
Mar 18, 2012 04:37 PM|LINK
Hi
I am trying to create a search engine from a grindview with no luck
this is the grindview i am using and the column i want to be search is the title , i am using vb an access vs 2010 ultimate
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Photo_ID" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Photo_Title" HeaderText="Title" SortExpression="Photo_Title" /> <asp:BoundField DataField="Photo_Category" HeaderText="Category" SortExpression="Photo_Category" /> <asp:ImageField DataImageUrlField="Photo_Image" HeaderText="Thumbnail"> <ControlStyle Height="100px" Width="100px" /> </asp:ImageField> <asp:HyperLinkField DataNavigateUrlFields="Photo_ID" DataNavigateUrlFormatString="detailsTwo.aspx?Photo_ID={0}" Text="more details" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Photo]"> </asp:SqlDataSource>
thanks for all the help
All-Star
97923 Points
14516 Posts
Mar 18, 2012 04:47 PM|LINK
Search in Gridview - http://www.codeproject.com/Articles/43018/ASP-NET-GridView-with-search-option-SearchableGrid
Thanks,
Mar 18, 2012 04:59 PM|LINK
i kinda wanted to do a search texbox with a button cos mine has an image field so the search needs to be on top
75950 Points
5608 Posts
Mar 21, 2012 07:39 AM|LINK
Hi,
Below are some of the more useful code-snippets used in the control to read/write data between the business objects and the
GridView
Search
public void Initialize() { BindDetailView(); ShowAdvancedSearch(false); } protected void BindDetailView() { CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders(); DataTable dtOrders = _Orders.GetDataTable("OrderID", "0"); dvOrders.DataSource = dtOrders; dvOrders.DataBind(); } protected void ShowAdvancedSearch(bool bShow) { pnlAdvancedSearch.Visible = bShow; pnlBasicSearch.Visible = !bShow; } protected string GetBasicSearchString() { StringBuilder sb = new StringBuilder(string.Empty); string sBasicSearchText = txtBasicSearch.Text.Trim(); if (sBasicSearchText == string.Empty) return string.Empty; string sCustomerID = GetSearchFormattedFieldString("CustomerID", sBasicSearchText, string.Empty, true); sb.Append(sCustomerID); sb.Append(" OR "); string sShipName = GetSearchFormattedFieldString("ShipName", sBasicSearchText, string.Empty, true); sb.Append(sShipName); sb.Append(" OR "); string sShipAddress = GetSearchFormattedFieldString("ShipAddress", sBasicSearchText, string.Empty, true); sb.Append(sShipAddress); sb.Append(" OR "); string sShipCity = GetSearchFormattedFieldString("ShipCity", sBasicSearchText, string.Empty, true); sb.Append(sShipCity); sb.Append(" OR "); string sShipRegion = GetSearchFormattedFieldString("ShipRegion", sBasicSearchText, string.Empty, true); sb.Append(sShipRegion); sb.Append(" OR "); string sShipPostalCode = GetSearchFormattedFieldString("ShipPostalCode", sBasicSearchText, string.Empty, true); sb.Append(sShipPostalCode); sb.Append(" OR "); string sShipCountry = GetSearchFormattedFieldString("ShipCountry", sBasicSearchText, string.Empty, true); sb.Append(sShipCountry); string sSearch = sb.ToString(); return sSearch; } protected string GetAdvancedSearchString() { StringBuilder sb = new StringBuilder(string.Empty); string sLastRowOperator = string.Empty; string sOrderID = GetSearchFormattedFieldString("OrderID", ((TextBox)dvOrders.FindControl("txtOrderID")).Text, ((DropDownList)dvOrders.FindControl( "ddlOrderIDOperator")).SelectedValue); sOrderID = sOrderID.Trim(); if (sOrderID != string.Empty) { if (sb.ToString().Trim() != string.Empty) sb.Append(" " + sLastRowOperator + " "); sb.Append(sOrderID); sLastRowOperator = ((DropDownList)dvOrders.FindControl( "ddlOrderIDRowOperator")).SelectedValue; } ... ... ... string sShipCountry = GetSearchFormattedFieldString("ShipCountry", ((TextBox)dvOrders.FindControl("txtShipCountry")).Text, ((DropDownList)dvOrders.FindControl( "ddlShipCountryOperator")).SelectedValue); sShipCountry = sShipCountry.Trim(); if (sShipCountry != string.Empty) { if (sb.ToString().Trim() != string.Empty) sb.Append(" " + sLastRowOperator + " "); sb.Append(sShipCountry); } string sSearch = sb.ToString(); return sSearch; } protected string GetSearchFormattedFieldString(string sfield, string value, string soperator) { return GetSearchFormattedFieldString(sfield, value, soperator, false); } protected string GetSearchFormattedFieldString(string sfield, string value, string soperator, bool bForceStringType) { if (value.Trim() == string.Empty) return string.Empty; string _newValue = value.Trim(); string _invalidChars = "`~!@#$%^&()-_=+[{]}\\|;:<,>./?\""; string _operator = "="; if(soperator != string.Empty) _operator = soperator; if (MSCD.Utilities.Validation.IsDateTime(_newValue) == false) { for (int i = 0; i < _invalidChars.Length; i++) { _newValue = _newValue.Replace(_invalidChars.Substring(i, 1), string.Empty); } _newValue = _newValue.Replace('*', '%'); _newValue = _newValue.Replace("'", "''"); } if (bForceStringType == false) { if (MSCD.Utilities.Validation.IsDateTime(_newValue) == true) { DateTime dtValue = DateTime.Parse(_newValue); _newValue = sfield + " " + _operator + " #" + dtValue.ToShortDateString() + "#"; } else if (MSCD.Utilities.Validation.IsNumeric(_newValue) == true) { _newValue = sfield + " " + _operator + " " + _newValue; } else { if(_newValue.IndexOf("%") > -1) _newValue = sfield + " LIKE '" + _newValue + "'"; else _newValue = sfield + " " + _operator + " '" + _newValue + "'"; } } else { if(_newValue.IndexOf("%") > -1) _newValue = sfield + " LIKE '" + _newValue + "'"; else _newValue = sfield + " " + _operator + " '" + _newValue + "'"; } return _newValue; }
Please check the following link:
http://www.codeproject.com/Articles/16781/GridView-Search-Control
http://weblogs.asp.net/dotnetstories/archive/2011/12/28/adding-search-functionality-in-a-gridview-in-an-asp-net-application.aspx
http://www.dotnetfunda.com/articles/article223.aspx
infernocy
Member
35 Points
67 Posts
gridview search engine
Mar 18, 2012 04:37 PM|LINK
Hi
I am trying to create a search engine from a grindview with no luck
this is the grindview i am using and the column i want to be search is the title , i am using vb an access vs 2010 ultimate
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Photo_ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Photo_Title" HeaderText="Title"
SortExpression="Photo_Title" />
<asp:BoundField DataField="Photo_Category" HeaderText="Category"
SortExpression="Photo_Category" />
<asp:ImageField DataImageUrlField="Photo_Image" HeaderText="Thumbnail">
<ControlStyle Height="100px" Width="100px" />
</asp:ImageField>
<asp:HyperLinkField DataNavigateUrlFields="Photo_ID"
DataNavigateUrlFormatString="detailsTwo.aspx?Photo_ID={0}" Text="more details" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [Photo]">
</asp:SqlDataSource>
thanks for all the help
ramiramilu
All-Star
97923 Points
14516 Posts
Re: gridview search engine
Mar 18, 2012 04:47 PM|LINK
Search in Gridview - http://www.codeproject.com/Articles/43018/ASP-NET-GridView-with-search-option-SearchableGrid
Thanks,
JumpStart
infernocy
Member
35 Points
67 Posts
Re: gridview search engine
Mar 18, 2012 04:59 PM|LINK
i kinda wanted to do a search texbox with a button cos mine has an image field so the search needs to be on top
Hua-Jun Li -...
All-Star
75950 Points
5608 Posts
Re: gridview search engine
Mar 21, 2012 07:39 AM|LINK
Hi,
Below are some of the more useful code-snippets used in the control to read/write data between the business objects and the
control and the control.public void Initialize() { BindDetailView(); ShowAdvancedSearch(false); } protected void BindDetailView() { CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders(); DataTable dtOrders = _Orders.GetDataTable("OrderID", "0"); dvOrders.DataSource = dtOrders; dvOrders.DataBind(); } protected void ShowAdvancedSearch(bool bShow) { pnlAdvancedSearch.Visible = bShow; pnlBasicSearch.Visible = !bShow; } protected string GetBasicSearchString() { StringBuilder sb = new StringBuilder(string.Empty); string sBasicSearchText = txtBasicSearch.Text.Trim(); if (sBasicSearchText == string.Empty) return string.Empty; string sCustomerID = GetSearchFormattedFieldString("CustomerID", sBasicSearchText, string.Empty, true); sb.Append(sCustomerID); sb.Append(" OR "); string sShipName = GetSearchFormattedFieldString("ShipName", sBasicSearchText, string.Empty, true); sb.Append(sShipName); sb.Append(" OR "); string sShipAddress = GetSearchFormattedFieldString("ShipAddress", sBasicSearchText, string.Empty, true); sb.Append(sShipAddress); sb.Append(" OR "); string sShipCity = GetSearchFormattedFieldString("ShipCity", sBasicSearchText, string.Empty, true); sb.Append(sShipCity); sb.Append(" OR "); string sShipRegion = GetSearchFormattedFieldString("ShipRegion", sBasicSearchText, string.Empty, true); sb.Append(sShipRegion); sb.Append(" OR "); string sShipPostalCode = GetSearchFormattedFieldString("ShipPostalCode", sBasicSearchText, string.Empty, true); sb.Append(sShipPostalCode); sb.Append(" OR "); string sShipCountry = GetSearchFormattedFieldString("ShipCountry", sBasicSearchText, string.Empty, true); sb.Append(sShipCountry); string sSearch = sb.ToString(); return sSearch; } protected string GetAdvancedSearchString() { StringBuilder sb = new StringBuilder(string.Empty); string sLastRowOperator = string.Empty; string sOrderID = GetSearchFormattedFieldString("OrderID", ((TextBox)dvOrders.FindControl("txtOrderID")).Text, ((DropDownList)dvOrders.FindControl( "ddlOrderIDOperator")).SelectedValue); sOrderID = sOrderID.Trim(); if (sOrderID != string.Empty) { if (sb.ToString().Trim() != string.Empty) sb.Append(" " + sLastRowOperator + " "); sb.Append(sOrderID); sLastRowOperator = ((DropDownList)dvOrders.FindControl( "ddlOrderIDRowOperator")).SelectedValue; } ... ... ... string sShipCountry = GetSearchFormattedFieldString("ShipCountry", ((TextBox)dvOrders.FindControl("txtShipCountry")).Text, ((DropDownList)dvOrders.FindControl( "ddlShipCountryOperator")).SelectedValue); sShipCountry = sShipCountry.Trim(); if (sShipCountry != string.Empty) { if (sb.ToString().Trim() != string.Empty) sb.Append(" " + sLastRowOperator + " "); sb.Append(sShipCountry); } string sSearch = sb.ToString(); return sSearch; } protected string GetSearchFormattedFieldString(string sfield, string value, string soperator) { return GetSearchFormattedFieldString(sfield, value, soperator, false); } protected string GetSearchFormattedFieldString(string sfield, string value, string soperator, bool bForceStringType) { if (value.Trim() == string.Empty) return string.Empty; string _newValue = value.Trim(); string _invalidChars = "`~!@#$%^&()-_=+[{]}\\|;:<,>./?\""; string _operator = "="; if(soperator != string.Empty) _operator = soperator; if (MSCD.Utilities.Validation.IsDateTime(_newValue) == false) { for (int i = 0; i < _invalidChars.Length; i++) { _newValue = _newValue.Replace(_invalidChars.Substring(i, 1), string.Empty); } _newValue = _newValue.Replace('*', '%'); _newValue = _newValue.Replace("'", "''"); } if (bForceStringType == false) { if (MSCD.Utilities.Validation.IsDateTime(_newValue) == true) { DateTime dtValue = DateTime.Parse(_newValue); _newValue = sfield + " " + _operator + " #" + dtValue.ToShortDateString() + "#"; } else if (MSCD.Utilities.Validation.IsNumeric(_newValue) == true) { _newValue = sfield + " " + _operator + " " + _newValue; } else { if(_newValue.IndexOf("%") > -1) _newValue = sfield + " LIKE '" + _newValue + "'"; else _newValue = sfield + " " + _operator + " '" + _newValue + "'"; } } else { if(_newValue.IndexOf("%") > -1) _newValue = sfield + " LIKE '" + _newValue + "'"; else _newValue = sfield + " " + _operator + " '" + _newValue + "'"; } return _newValue; }Please check the following link:
http://www.codeproject.com/Articles/16781/GridView-Search-Control
http://weblogs.asp.net/dotnetstories/archive/2011/12/28/adding-search-functionality-in-a-gridview-in-an-asp-net-application.aspx
http://www.dotnetfunda.com/articles/article223.aspx
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework