Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Clear GridView Dataset

Posted on 2012-12-26
14
Medium Priority
?
453 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

688 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