Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've been trying to make my datagridview filter by using a drop down in the header. I found a post on this site that had a solution Here, but it required me to have the ajax control toolkit which I am not permitted to download on the company computer. Are there any other solutions to this problem?

Please note that I am not a strong programmer yet so please be detailed in your replies.

Please also note that the gridview datasource is from a different server than the code.

Thanks in advance,
Sean8084
Posted
Updated 8-Sep-11 10:34am
v3

Sean, we need to simulate the functionality ajax control toolkit in ASP .NET

Herez the sample code with EmployeeID and Name.

XML
<body>
    <form id="form1" runat="server">
    <div>
      <table style="width: 640px" border="0" cellpadding="0" cellspacing="6" class="GridviewTable">
        <tr >
            <td style="width: 120px">
                Employee ID
            </td>
            <td style="width: 120px">
                Employee Name
            </td>
         </tr>
          <tr >
                <td style="width: 120px;">
                    <asp:TextBox ID="txtEmpId" runat="server" Width="75px"></asp:TextBox>
                    <asp:Button ID="btnEmpId" runat="server" Text="IDFilter" onclick="btnIdClick" />
                </td>
                <td style="width: 120px;">
                    <asp:TextBox ID="txtEmpName" runat="server" Width="75px"></asp:TextBox>
                    <asp:Button ID="btnEmpName" runat="server" Text="NameFilter" onclick="btnNameClick" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:GridView ID="gvEmp" runat="server" AutoGenerateColumns="False" Width="640px" ShowHeader="False" >
                        <Columns>
                            <asp:BoundField DataField="EmpId" ItemStyle-Width="120px" >
                            <ItemStyle Width="120px"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="EmpName" ItemStyle-Width="120px" >
                            <ItemStyle Width="120px"></ItemStyle>
                            </asp:BoundField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>


In code behind System.Data.DataTable plays the major role. Generate the local data table and change the result set during filter button clicked event.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        EmpTable = new EmpTable();
        empTable = CreateDataTable();
        Session["EmpTable"] = empTable;

        this.gvEmp.DataSource = ((DataTable)Session["EmpTable"]).DefaultView;
        this.gvEmp.DataBind();
    }
}

private DataTable CreateDataTable()
{
    DataTable myDataTable = new DataTable();
    DataColumn myDataColumn;

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "EmpId";
    myDataTable.Columns.Add(myDataColumn);


    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "EmpId";
    myDataTable.Columns.Add(myDataColumn);

    myDataTable.Rows.Add("111", "aaaaaa");

    return myDataTable;
}

protected void btnIdClick(object sender, EventArgs e)
{
    if (txtEmpId.Text.ToString().Trim() != "")
    {
        DataTable dt = (DataTable)Session["EmpTable"];
        var linquery = from tbl in dt.AsEnumerable()
                    where tbl.Field("EmpId").StartsWith(txtProductId.Text.ToString().Trim())
                    select t;
        DataTable dtable = new DataTable();
        dtable = linquery.CopyToDataTable();
        this.gvEmp.DataSource = dtable;
        this.gvEmp.DataBind();
    }
    else
    {
        this.gvEmp.DataSource = ((DataTable)Session["EmpTable"]).DefaultView;
        this.gvEmp.DataBind();
    }
}
 
Share this answer
 
Comments
Sean8084 9-Sep-11 13:09pm    
I've built this all in the ASP portion but I've got 15 columns, 4 of which are supposed to filter. once I build it, it has all the 15 column headers on top, as they should be, but the filters are stretched out one on top of the other with the gridview below it. how can I align the filter cells with their correct column?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900