0
votes

Anyone know how to actually filter a gridview that is based on a datable (which datatable is based on sql data source)?

This is how my gridview works:

  1. The gridview is dynamically created based on inputted data source, database name, and table name

    asp:TextBox ID="server_tb" runat="server"

    asp:TextBox ID="user_tb" runat="server"

    asp:TextBox ID="pswrd_tb" runat="server" TextMode="Password"

    asp:TextBox ID="database_tb" runat="server"

    asp:TextBox ID="table_tb" runat="server"

  2. Using the input I called the correct table from database and then populated it into datatable

    DataTable Table = new DataTable();

    Connection = new System.Data.SqlClient.SqlConnection("Data Source=" + ServerName + ";Initial Catalog=" + DatabaseName + ";User ID=" + UserName + ";Password=" + Password + "; Connect Timeout=120");

  3. I bind datatable and dynamically created button template into the gridview together (button template is to allow editing, adding, and deleting)

    gvGridView.Columns.Add(ItemTmpField);

    vGridView.DataSource = Table;

    gvGridView.DataBind();

This is how basically I roughly created my GridView, the code is working well. Basically all I need is to allow filtering on it only. And I could really used some advices on this.

1

1 Answers

1
votes

You can filter as below:

DataRow[] rows = dTable.Select(" user_tb > 5"); // default
DataRow[] rows1 = dTable.Select(" user_tb > 5", "user_tb ASC"); // with sorting

You can see a good article here