views:

3426

answers:

3

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>
+2  A: 

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>
Enrico Campidoglio
i have similar question, can you help me here http://stackoverflow.com/questions/3356900/linqdatasource-gridview-filtering-with-dropdownlist
Abu Hamzah
I posted an answer to your question: http://stackoverflow.com/questions/3355254/linqdatasource-filtering-and-binding-to-gridview/3400433#3400433
Enrico Campidoglio
A: 

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}
       ;
 }
Nicholas
A: 

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(); } }