MrProgram
MrProgram

Reputation: 5242

Filter gridview datasource

I wan't to be able to search through my gridview result using code-behind.

I have a button and a gridview:

<asp:TextBox ID="txtPaperId" Width="146" runat="server"  />
<asp:Button runat="server" ID="btnSearch" Text="Search" OnClick="FilterResult"/>

<asp:GridView ID="gvwResavePositions" runat="server" EmptyDataText="No Positions found!"
    AllowPaging="True" AllowSorting="True" PageSize="50" AutoGenerateColumns="False"
    SkinID="gridviewGridlinesSkin" HeaderStyle-HorizontalAlign="Left" 
    OnPageIndexChanging="gvwResavePositions_PageIndexChanged"
    HorizontalAlign="Left" Width="100%">
        <HeaderStyle BackColor="DarkGray" Font-Bold="True" HorizontalAlign="left"  />
        <RowStyle HorizontalAlign="Left" />
        <Columns>
            <asp:BoundField  ItemStyle-Width="40px" DataField="strPaperId" HeaderText="K+Id / PaperId" ReadOnly="True"/>
            <asp:TemplateField HeaderText="Resave">
                <ItemTemplate>
                    <asp:CheckBox ID="bResave" runat="server" Width="50"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle HorizontalAlign="Left" />
    </asp:GridView>

My code behind:

protected void FilterResult(object sender, EventArgs e)
{
    try
    {
        (gvwResavePositions.DataSource as DataTable).DefaultView.RowFilter = string.Format("strPaperId = '{0}'",
            txtPaperId.Text);
    }
    catch (Exception ex)
    {
        var t = ex.Message;
    }
}

When I press "Search" button I get "Object reference not set to an instance of an object."

The objectdatasource is set before the search button is pressed (it's another button...), so when FilterResult is going the gridview is full of results..

Do you have any tip how I can do this?

EDIT

Solved it. It was because my datasource was null, so I had to rebind it.

Upvotes: 2

Views: 7509

Answers (2)

user2153378
user2153378

Reputation:

You should use a dataview rowfilter.

Update: you must use the correct parameters. With the rowfilter it is also possible to filter on already filtered items (it was your next question).

Or there also this possibility:

string strWhere = string.empty
foreach (string id in ids)
{
string += id + " OR " 
}

string += 0

Upvotes: 1

Dgan
Dgan

Reputation: 10275

Try this:

// save your datatable in session while binding gridview
    // Session["Dt_GridView"]=Your_datatable; 
    protected void FilterResult(object sender, EventArgs e)
    {
        try
        {
           // DataTable dt = (DataTable)gvwResavePositions.DataSource; this reutrn null
            // hence
            //gvwResavePositions.DataSource as DataTable this will return null



            DataTable dt = (DataTable)Session["Dt_GridView"];

         dt.DefaultView.RowFilter = string.Format("strPaperId = '{0}'",
                txtPaperId.Text);
         gvwResavePositions.DataSource = dt;
          gvwResavePositions.DataBind();
            }
        catch (Exception ex)
        {
            var t = ex.Message;
        }
    }

Upvotes: 3

Related Questions