Nicholas
Nicholas

Reputation: 1750

Filter Gridview results using Linq to Sql Object datasource with a LIKE operator

I have a gridview bound to a LINQ to Sql datasource. I would like to filter the the results in the gridview using the LIKE operator. i.e I have a textbox used to Search on Username and I would like to select all users with the username like [textbox value].

Below is my code:

<h1>Manage Users</h1>
Search for users Username:
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1">
    <Columns>
        <asp:BoundField DataField="UserName" HeaderText="User Name" ReadOnly="True" 
            SortExpression="UserName" />
        <asp:BoundField DataField="FullName" HeaderText="Full Name" ReadOnly="True" 
            SortExpression="FullName" />
        <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" 
            SortExpression="Email" />
       <asp:BoundField DataField="LastLoginDate" HeaderText="Last Login" ReadOnly="True" 
            SortExpression="LastLoginDate" DataFormatString="{0:dd MMMM yyyy}"/>
       <asp:HyperLinkField Text="Edit" DataNavigateUrlFields="UserId" DataNavigateUrlFormatString="~/Pages/UsersMaintenance/CreateEditUser.aspx?UserId={0}" />
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AirProducts.BusinessLogic.AirProductsDataContext" 

    Select="new (UserId,UserName, Details.FullName,Membership.Email,Membership.LastLoginDate)" 
    TableName="Users" Where="UserName == @UserName" >
    <WhereParameters>
        <asp:ControlParameter ControlID="txtUsername" Name="UserName" 
            PropertyName="Text" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>

Upvotes: 1

Views: 8337

Answers (3)

Nicholas
Nicholas

Reputation: 1750

I couldn't get it to work in the markup of the LinqDataSource, but was able to trap it here, plus as a bonus I got intellisense support.

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    AirProductsDataContext db = new AirProductsDataContext();
    e.Result = from v in db.Users orderby v.UserName ascending
               where v.UserName.Contains(txtUsername.Text)
               select new {v.UserId,v.UserName, v.UserDetail.FullName,v.Membership.Email,v.Membership.LastLoginDate};
}

Upvotes: 0

sridhar
sridhar

Reputation:

You can get information your requirement please refer this link www.delicious.com/sridharnetha

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
  if (e.CommandName.Equals("AddNew"))
  {
   TextBox txtNewName=(TextBox)GridView1.FooterRow.FindControl("txtNewName"); 
   DropDownList cmbNewGender = (DropDownList)GridView1.FooterRow.FindControl("cmbNewGender"); 
   TextBox txtNewCity = (TextBox)GridView1.FooterRow.FindControl("txtNewCity"); 
   TextBox txtNewState = (TextBox)GridView1.FooterRow.FindControl("txtNewState"); 
   DropDownList cmbNewType = (DropDownList)GridView1.FooterRow.FindControl("cmbNewType");

   customer.Insert(txtNewName.Text, cmbNewGender.SelectedValue, txtNewCity.Text, txtNewState.Text, cmbNewType.SelectedValue) ; 
   FillCustomerInGrid();
  }
} 

Upvotes: 0

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59885

Substitute the equals operator with a call to the String.Contains method in the Where clause of your LINQ expression:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AirProducts.BusinessLogic.AirProductsDataContext" 

        Select="new (UserId,UserName, Details.FullName,Membership.Email,Membership.LastLoginDate)" 
        TableName="Users" Where="UserName.Contains(@UserName)" >
        <WhereParameters>
                <asp:ControlParameter ControlID="txtUsername" Name="UserName" 
                        PropertyName="Text" Type="String" />
        </WhereParameters>
</asp:LinqDataSource>

Upvotes: 2

Related Questions