Solved

Clear GridView Dataset

Posted on 2012-12-26
14
445 Views
Last Modified: 2013-01-02
Hello:

I have a textbox that searches a gridview. When I clear the results from a search and do another search, the results from the previous search shows. LastNameSearch is the textbox that searches the GridView.

    protected void GridSearch_Click(object sender, EventArgs e)
    {
        BindGridView1();
    }
    protected void GridSearchClear_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        LastNameSearch.Text = "";
    }

<asp:Button ID="GridSearch" Text="Search" runat="server" onclick="GridSearch_Click" />
<asp:Button ID="GridSearchClear" Text="Clear" runat="server" onclick="SearchClear_Click" />

Open in new window

0
Comment
Question by:RecipeDan
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 38722547
Hi RecipeDan,

I'd suggest to look into BindGridView1(); method. You might have cached the search result somewhere and on PageLoad you might be binding it to the GridView again.

Regards,
Chinmay.
0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38722578
call databind after setting null,
    GridView1.DataSource = null;
        GridView1.DataBind();

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 38722714
@Meetan03...i tried that and it still doesn't work

@Chinmay...is onclick page_load or a postback? I have both on the page.

Dan
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 38722744
It is on page load, postback is just a check you perform to identify if the page is loading again in response of a user action or not. So I'd suggest you check on Page Load first.

Copy paste content of the Page_Load here so I can have a good look at it.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38722805
try this

GridView1.DataSource=null;
GridView1.DataBind();
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38722807
I mean in this function
before bind new data

 protected void GridSearch_Click(object sender, EventArgs e)
    {
GridView1.DataSource=null;
GridView1.DataBind();
        BindGridView1();
    }
0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38722927
if your loading gridview on pageload then do check with line .
If (!Ispostback){bindgrid();}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:RecipeDan
ID: 38724254
Hello:

I tried the suggestions and still get the same result:

Here is the whole script in succession:

<asp:ImageButton ID="ImgPanel"  ImageUrl="~/Images/quesmarkicon.png" runat="server" onclick="ImgPanel_Click" />
    
protected void ImgPanel_Click(object sender, ImageClickEventArgs e)
    {
        GridPanel.Visible = true;
        ClosePanel.Visible = true;
        LastNameSearchLabel.Visible = true;
        LastNameSearch.Visible = true;
        GridSearch.Visible = true;
        GridSearchClear.Visible = true;
    }
<asp:Panel ID="GridPanel"  runat="server">
          <asp:ImageButton ID="ClosePanel" Visible="false" onclick="ClosePanel_Click" ImageAlign="Right" ImageUrl="~/Images/Close.gif" runat="server" />
          <br />
          <table>
          <tr>
          <td>
          <asp:Label ID="LastNameSearchLabel" Visible="false" runat="server" Text="Enter Last Name: " />
          </td>
          <td>
          <asp:TextBox ID="LastNameSearch" Visible="false" runat="server" />
          </td>
          </tr>
          <tr>
          <td align="center" colspan="2">
          <asp:Button ID="GridSearch" Visible="false" Text="Search" runat="server" onclick="GridSearch_Click" />
          &nbsp;
          <asp:Button ID="GridSearchClear" Visible="false" Text="Clear" runat="server" onclick="GridSearchClear_Click" />
          </td>
          </tr>
          </table>
          <br />
          <asp:GridView ID="GridView1" GridLines="None" runat="server" OnSorting="GridView1_Sorting" 
          OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" 
          PageSize="10" AutoGenerateColumns="false" AllowSorting="True"> 
          <Columns>
          <asp:TemplateField HeaderText="Select">
          <ItemTemplate>
          <asp:CheckBox id="CStatus" AutoPostBack="true" OnCheckedChanged="CStatus_OnCheckedChanged" runat="server" />
          </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="PName" SortExpression="PName" HeaderText="Name" />
          <asp:BoundField DataField="UserEMailAddress" SortExpression="UserEMailAddress" HeaderText="E-Mail" />
          </Columns>
          </asp:GridView> 
 </asp:Panel>            
protected void GridSearch_Click(object sender, EventArgs e)
    {
	GridView1.DataSource=null;
	GridView1.DataBind(); 
	BindGridView1();
    }
    protected void GridSearchClear_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        LastNameSearch.Text = "";
    }

Open in new window

0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38724516
 protected void GridSearchClear_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();//Still Missing on your code 
        LastNameSearch.Text = "";
    }

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 38724601
Still the same result...
0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38724642
try this sample  and check what your missing on your code .
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ImageButton ID="ImgPanel"  ImageUrl="~/Images/quesmarkicon.png" runat="server" AlternateText="Search" onclick="ImgPanel_Click" />
    
<asp:Panel ID="GridPanel"  runat="server">
<%--          <asp:ImageButton ID="ClosePanel" Visible="false" onclick="ClosePanel_Click" ImageAlign="Right" ImageUrl="~/Images/Close.gif" runat="server" />
--%>
          <br />
          <table>
          <tr>
          <td>
          <asp:Label ID="LastNameSearchLabel" Visible="false" runat="server" Text="Enter Last Name: " />
          </td>
          <td>
          <asp:TextBox ID="LastNameSearch" Visible="false" runat="server" />
          </td>
          </tr>
          <tr>
          <td align="center" colspan="2">
          <asp:Button ID="GridSearch" Visible="false" Text="Search" runat="server" onclick="GridSearch_Click" />
          &nbsp;
          <asp:Button ID="GridSearchClear" Visible="false" Text="Clear" runat="server" onclick="GridSearchClear_Click" />
          </td>
          </tr>
          </table>
          <br />
          <asp:GridView ID="GridView1" GridLines="None" runat="server" 
          PageSize="10" AutoGenerateColumns="true" > 
          
          </asp:GridView> 
 </asp:Panel>            

    </div>
    </form>
</body>
</html>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridSearch_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
        BindGridView1();
    }
    protected void GridSearchClear_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
        LastNameSearch.Text = "";
    }
    protected void ImgPanel_Click(object sender, ImageClickEventArgs e)
    {
        GridPanel.Visible = true;
       // ClosePanel.Visible = true;
        LastNameSearchLabel.Visible = true;
        LastNameSearch.Visible = true;
        GridSearch.Visible = true;
        GridSearchClear.Visible = true;
    }

    public void  BindGridView1()
    {
        
        GridView1.DataSource = GetTable();//Temporary Dataset
        GridView1.DataBind();
          
    }
    static DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        //
        // Here we add five DataRows.
        //
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }


}

Open in new window


Meeran03
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 38724862
It works but it lists all records in the temporary table. If I type David the record for David should show. Then I press Clear and type Sam. Sam's record should show.

That is not happening in my original question. For example, I would type David and David's record lists. then I press clear and type Sam. David's record would still show.
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 38729018
Hi RecipeDan,

"..then I press clear"
   does  the grid clears on this action ?

" David's record would still show "
  Your retrieval query might have issue .  

Try this, It has search option  and do the clear part well .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ImageButton ID="ImgPanel"  ImageUrl="~/Images/quesmarkicon.png" runat="server" AlternateText="Search" onclick="ImgPanel_Click" />
    
<asp:Panel ID="GridPanel"  runat="server">
<%--          <asp:ImageButton ID="ClosePanel" Visible="false" onclick="ClosePanel_Click" ImageAlign="Right" ImageUrl="~/Images/Close.gif" runat="server" />
--%>
          <br />
          <table>
          <tr>
          <td>
          <asp:Label ID="LastNameSearchLabel" Visible="false" runat="server" Text="Enter Last Name: " />
          </td>
          <td>
          <asp:TextBox ID="LastNameSearch" Visible="false" runat="server" />
          </td>
          </tr>
          <tr>
          <td align="center" colspan="2">
          <asp:Button ID="GridSearch" Visible="false" Text="Search" runat="server" onclick="GridSearch_Click" />
          &nbsp;
          <asp:Button ID="GridSearchClear" Visible="false" Text="Clear" runat="server" onclick="GridSearchClear_Click" />
          </td>
          </tr>
          </table>
          <br />
          <asp:GridView ID="GridView1" GridLines="None" runat="server" 
          PageSize="10" AutoGenerateColumns="true" > 
          
          </asp:GridView> 
 </asp:Panel>            

    </div>
    </form>
</body>
</html>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridSearch_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
        BindGridView1(LastNameSearch.Text);
    }
    protected void GridSearchClear_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
        LastNameSearch.Text = "";
    }
    protected void ImgPanel_Click(object sender, ImageClickEventArgs e)
    {
        GridPanel.Visible = true;
       // ClosePanel.Visible = true;
        LastNameSearchLabel.Visible = true;
        LastNameSearch.Visible = true;
        GridSearch.Visible = true;
        GridSearchClear.Visible = true;
    }

    public void  BindGridView1(string searchTerm)
    {

        DataTable dt = GetTable();
 
        if (searchTerm != ""){
            
            DataRow[] dr =  dt.Select("Patient like '%" + searchTerm  + "%'");
            if (dr.Length > 0) {
                dt = null;
                dt = dr.CopyToDataTable();
            }

        }
        GridView1.DataSource = dt;//Temporary Dataset
        GridView1.DataBind();
          
    }
    static DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        //
        // Here we add five DataRows.
        //
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }


}

Open in new window

0
 
LVL 1

Author Closing Comment

by:RecipeDan
ID: 38738237
Sorry I was out of town for New Years. It works great! Thank you for your help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now