Solved

Clear GridView Dataset

Posted on 2012-12-26
14
449 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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