Solved

Clear GridView Dataset

Posted on 2012-12-26
14
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

632 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