Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview search boxes

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
Hello All

I have a project that I'd like to set up a gridview with text boxes at the top of each column.
These will be used to search or filter the gridview by it's columns.

Do these have to be wired manually or is there a better way?
I tried using the SqlDataSource where you pick the control and it filters the grid but I'm getting weird errors and can't trap them.

I looked on the net to see what others are doing but I don't know what this scenario is called.

Any help would be great.

 
On the search pages I have written, I don't include textboxes in the grid. I have one section of the page that shows input controls for search criteria. Then, another section that shows the results. I like to use a Multiview control for this. Then if the user wants to filter more or less, they can just simply change the search criteria and submit again.
 
optoins.

1. use a 3rd party grid that has this feature. $$$
2. build it yourself. cheaper, but more hours.

which a solution like this you are much better off not using any of the datasource controls. instead use the tried and true manual binding (this also means you need to wireup the sorting/paging events as well.)
Code:
<asp:gridview>
   <columns>
      <asp:TemplateColumn>
         <HeaderTemplate>
            <asp:TextBox ID="ColumnA" CommandName="Search" />
         </HeaderTemplate>
         <ItemTemplate>
            <%#Bind("SomeProperty")%>
         </ItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn>
         <HeaderTemplate>
            <asp:TextBox ID="ColumnB" CommandName="Search" />
         </HeaderTemplate>
         <ItemTemplate>
            <%#Bind("SomeOtherProperty")%>
         </ItemTemplate>
      </asp:TemplateColumn>
   </columns>
</asp:gridview>
Code:
private string userDefinedFilter = string.Empty;

override OnInit(EventArgs e)
{
   myGrid.RowCommand += ExecuteCommand;
}

override OnLoad(EventArgs e)
{
   if (!IsPostback) LoadData();
}

void ExecuteCommand(object sender, GridViewRowCommandEventArgs e)
{
   switch(e.CommandName)
   {
      case "Search":
         TextBox textbox = (TextBox)sender;
         userDefinedFilter = string.Format("[{0}] = '{1}'", textbox.ID, textbox.Text);
         break;
   }
   LoadData();
}

LoadData()
{
   MyGridView.DataSource = GetData(userDefinedFilter);
   MyGridView.DataBind();
}
GetData will query the database and use the filter passed in by the user. this is just one of many ways to complete the task.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
jbenson001's solution is easier to program:)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top