Solved

Clear GridView Dataset

Posted on 2012-12-26
14
450 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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