Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using asp.net application.

I'm having one gridview in source code... and it is filled with dataset in page load.
my question is when I'm rebinding data to the same gridview.. I'm able to see two gridviews in
my solution(application on execution) .. I'm clearing the gridview (with gv.datasouce =null; and databind) but still its not getting cleared


protected void Page_Load(object sender, EventArgs e)
 { try
 { if (Session["School_ID"] != null && Int32.TryParse(Session["School_ID"].ToString(), out school_ID) && Session["LoginId"] != null && Int32.TryParse(Session["LoginId"].ToString(), out loginId)) { school_ID = Convert.ToInt32(Session["School_ID"]); loginId = Convert.ToInt32(Session["LoginId"]); } else { Response.Redirect("~/SessionExpired.aspx"); } if (!IsPostBack) { Label lblHeader = (Label)Page.Master.FindControl("lblHeaderName"); lblHeader.Text = "Time Table"; bindmedium(); ddlClass.Enabled = false; ddlMedium.Enabled = false; ddlSection.Enabled = false; lblcount1.Visible = false; btnsave.Visible = false; ddlSelection.Enabled = true; ddlWeekdays.Enabled = true; btnupdate.Visible = false; bindWeekDays(); ddlSelection.SelectedIndex = 0; bindcommon(); } } catch (Exception ex) { ExceptionHandler CEObj = new ExceptionHandler(ex); } } public void bindcommon() { try { gvTimeTable.DataSource = null; gvTimeTable.DataBind(); ObjCTT = new ClassTimetable(); ObjCTT.School_ID = school_ID; ObjCTT.stype = false; ObjCTT.weekday = 0; ObjDataset = ObjCTT.GetTimeTable(); gvTimeTable.DataSource = ObjDataset.Tables[0]; gvTimeTable.DataBind(); ObjDataset = null; } catch (Exception ex) { ExceptionHandler CEObj = new ExceptionHandler(ex); } finally { ObjDataset = null; } } protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e) { try { lblmessage.Text = ""; if (ddlSection.SelectedIndex != 0) { btnAdd.Visible = true; } else { btnAdd.Visible = false; } btnupdate.Visible = false; btnsave.Visible = false; ddlWeekdays.Enabled = true; gvTimeTable.DataSource = null; gvTimeTable.DataBind(); ObjCTT = new ClassTimetable(); ObjCTT.School_ID = school_ID; ObjCTT.weekday = Convert.ToInt32(ddlWeekdays.SelectedValue); ObjCTT.stype = true; ObjCTT.MCID = Convert.ToInt32(ddlSection.SelectedValue); ObjDataset = ObjCTT.GetTimeTable(); ViewState["gv"] = ObjDataset.Tables[0]; gvTimeTable.DataSource = ObjDataset.Tables[0]; ViewState["Data1"] = (DataTable)ObjDataset.Tables[0]; gvTimeTable.DataBind(); SetPeriodBreak(ObjDataset.Tables[0]); SetFromTime(); bindWeekDays(); } catch (Exception ex) { ExceptionHandler CEObj = new ExceptionHandler(ex); } }
Posted
Updated 21-Dec-13 1:21am
v5

try this code


C#
dgrid.rows.clear();
dgrid.columns.clear();
 
Share this answer
 
Comments
Member 10468587 16-Dec-13 1:56am    
Mr.Joy not yet checked..

I will let you know with in a short span weather your piece of code is working or not.

But However Thanks for Your concern
An@nd Rajan10 16-Dec-13 2:02am    
ok dude...
Member 10468587 16-Dec-13 5:36am    
there is no method clear for gridview.Rows
An@nd Rajan10 16-Dec-13 5:49am    
you tried this..
Member 10468587 16-Dec-13 6:37am    
yes ofcourse... butt just just that is not a valid method how can i implement
i wrote this in vb so it is just like a C#
from your question i assume this so please write your code if this is not what you want???
'for VB

FOR CONNECTION

Public cn As New SqlClient.SqlConnection
Public Sub cnopen(){
        
        Dim ConnectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\blabla.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=true;"
        cn = New SqlClient.SqlConnection(ConnectionString)
        cn.Open()
    End Sub

}

sub selectDataFromDatabase(){ 'subroutine to trigger
 
Dim dataFile As New DataSet 'creating new dataset

qry = "select * from `tablename`" 'query string

Dim cmd As New SqlClient.SqlCommand(qry, cn) 'qry="query string" cn= connection

Dim da As New SqlClient.SqlDataAdapter(cmd)

dataFile.Clear()

da.Fill(dataFile)

dataLists.DataSource = dataFile.Tables(0) 'here datalist is a datagridview
}

'to clear the datagridview
sub clearGrid(){
dataLists.columns.clear() 'you don't have to clear the row only columns clear will clear the datagridview
}
 
'how to use it
'to select the data
selectDataFromDatabase()
 
'to clear grid
clearGrid()
 
//
For C#
void selectDataFromDatabase(){ 'subroutine to trigger

                Dim dataFile As New DataSet; 'creating new dataset

      qry = "select * from `tablename`;" 'query string

      Dim cmd As New SqlClient.SqlCommand(qry, cn); 'qry="query string" cn= connection

      Dim da As New SqlClient.SqlDataAdapter(cmd);
 
      dataFile.Clear();
 
      da.Fill(dataFile);
 
      dataLists.DataSource = dataFile.Tables(0) 'here datalist is a datagridview;
}
 
'to clear the datagridview
void clearGrid(){
dataLists.columns.clear(); 'you don't have to clear the row only columns clear will clear the datagridview
}

 

 
if this is not what you want that please send your code i will look at it
 
Share this answer
 
Please paste your code and let us have a look at it.
Are you calling the bind grid method in page load if you are calling it please place the function call in if(!IsPostBack){

}
block

As you said you are clearing the data then this is where I think the issue exists.
 
Share this answer
 
Comments
Member 10468587 16-Dec-13 7:59am    
protected void Page_Load(object sender, EventArgs e)
{
try
{

if (Session["School_ID"] != null && Int32.TryParse(Session["School_ID"].ToString(), out school_ID) && Session["LoginId"] != null && Int32.TryParse(Session["LoginId"].ToString(), out loginId))
{
school_ID = Convert.ToInt32(Session["School_ID"]);
loginId = Convert.ToInt32(Session["LoginId"]);
}
else
{
Response.Redirect("~/SessionExpired.aspx");
}
if (!IsPostBack)
{

Label lblHeader = (Label)Page.Master.FindControl("lblHeaderName");
lblHeader.Text = "Time Table";
bindmedium();
ddlClass.Enabled = false;
ddlMedium.Enabled = false;
ddlSection.Enabled = false;
lblcount1.Visible = false;
btnsave.Visible = false;
ddlSelection.Enabled = true;
ddlWeekdays.Enabled = true;
btnupdate.Visible = false;
bindWeekDays();
ddlSelection.SelectedIndex = 0;
bindcommon();

}
}

catch (Exception ex)
{
ExceptionHandler CEObj = new ExceptionHandler(ex);

}
}

public void bindcommon()
{
try
{

gvTimeTable.DataSource = null;
gvTimeTable.DataBind();
ObjCTT = new ClassTimetable();
ObjCTT.School_ID = school_ID;
ObjCTT.stype = false;
ObjCTT.weekday = 0;
ObjDataset = ObjCTT.GetTimeTable();
gvTimeTable.DataSource = ObjDataset.Tables[0];
gvTimeTable.DataBind();
ObjDataset = null;
}
catch (Exception ex)
{
ExceptionHandler CEObj = new ExceptionHandler(ex);

}
finally
{

ObjDataset = null;

}
}

protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
lblmessage.Text = "";
if (ddlSection.SelectedIndex != 0)
{
btnAdd.Visible = true;
}
else
{
btnAdd.Visible = false;
}
btnupdate.Visible = false;
btnsave.Visible = false;
ddlWeekdays.Enabled = true;
gvTimeTable.DataSource = null;
gvTimeTable.DataBind();
ObjCTT = new ClassTimetable();
ObjCTT.School_ID = school_ID;
ObjCTT.weekday = Convert.ToInt32(ddlWeekdays.SelectedValue);
ObjCTT.stype = true;
ObjCTT.MCID = Convert.ToInt32(ddlSection.SelectedValue);
ObjDataset = ObjCTT.GetTimeTable();
ViewState["gv"] = ObjDataset.Tables[0];
gvTimeTable.DataSource = ObjDataset.Tables[0];
ViewState["Data1"] = (DataTable)ObjDataset.Tables[0];
gvTimeTable.DataBind();
SetPeriodBreak(ObjDataset.Tables[0]);
SetFromTime();
bindWeekDays();

}

catch (Exception ex)
{
ExceptionHandler CEObj = new ExceptionHandler(ex);

}
}
Member 10468587 16-Dec-13 8:04am    
when im filling my gridview with another dataset ...


i'm able to view two grids for a single grid view
Member 10468587 16-Dec-13 8:05am    
<asp:GridView ID="gvTimeTable" runat="server" AutoGenerateColumns="False" CssClass="gridview"
Width="100%">
<columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left"
HeaderStyle-Width="23%" ItemStyle-Width="23%">
<HeaderTemplate>
<asp:Label ID="lblperiodHd" runat="server" Text="<%$ Resources:LocalStrings,Period %>">
</HeaderTemplate>
<itemtemplate>
<asp:Label ID="lblPeriodName" runat="Server" CssClass="text" Text='<%# Eval("Periodtype1") %>'>
<asp:Label ID="lblperiodnumber" runat="server" Visible="true" Text='<%# Eval("PeriodNumberr") %>'>

<footerstyle horizontalalign="Left">
<HeaderStyle HorizontalAlign="left" />
<itemstyle horizontalalign="left">

<asp:TemplateField HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left"
HeaderStyle-Width="23%" ItemStyle-Width="23%">
<HeaderTemplate>
<asp:Label ID="lblstrtHr" runat="server" Text="<%$ Resources:LocalStrings, StartTime %>">
</HeaderTemplate>
<itemtemplate>
<asp:Label ID="lblphf" runat="Server" CssClass="text" Text='<%# Eval("PeriodHourF") %>'>:
<asp:Label ID="lblpmf" runat="Server" CssClass="text" Text='<%# Eval("PeriodMinsF") %>'>
<asp:Label ID="lblpapmf" runat="Server" CssClass="text" Text='<%# Eval("Period_ApmF") %>'>

<footerstyle horizontalalign="Left">
<HeaderStyle HorizontalAlign="left" />
<itemstyle horizontalalign="left">

<asp:TemplateField HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left"
HeaderStyle-Width="23%" ItemStyle-Width="23%">
<HeaderTemplate>
<asp:Label ID="lblEt" runat="server" Text="<%$ Resources:LocalStrings, EndTime %>">
</HeaderTemplate>
<itemtemplate>
<asp:Label ID="lblpht" runat="Server" CssClass="text" Text='<%# Eval("PeriodHourT") %>'>:
<asp:Label ID="lblpmt" runat="Server" CssClass="text" Text='<%# Eval("PeriodMinsT") %>'>
<asp:Label ID="lblpapmt" runat="Server" CssClass="text" Text='<%# Eval("Period_ApmT") %>'>

<footerstyle horizontalalign="Left">
<HeaderStyle HorizontalAlign="left" />
<itemstyle horizontalalign="left">

<asp:TemplateFi
[no name] 16-Dec-13 8:24am    
What does bindmedium function in your code do?
Member 10468587 17-Dec-13 1:29am    
bindmedium function just binds values to ddl(dropdownlist)

sry for delay
this problem i face also :D
it just to have to clear the dataset first and clear the grid.

that's all folks :)
 
Share this answer
 
Comments
Member 10468587 16-Dec-13 1:53am    
In fact I'm Clearing the DataSet Too....
and even setting gridview datasource to null
Member 10468587 16-Dec-13 6:36am    
problem not yet solved
Member 10468587 16-Dec-13 6:54am    
any more ideas
Try like this,

after clearing the datasource you have to rebind the control, to get reflect in the screen.

C#
GridView1.DataSource = null;

GridView1.DataBind();
 
Share this answer
 
Comments
Member 10468587 16-Dec-13 8:26am    
thts wt i had done but still no use
Karthik_Mahalingam 16-Dec-13 8:51am    
Gridview1.visible = false;

is this working ??
Member 10468587 17-Dec-13 5:59am    
if i do so my grid view goes to invisible but actually im rebinding the data to same grid

please try to understand the question
Karthik_Mahalingam 17-Dec-13 6:00am    
no , i just want to confirm that the changes are reflecting on the screen.
Karthik_Mahalingam 17-Dec-13 6:02am    
GridView1.DataSource = null;
GridView1.DataBind();
GridView1.DataSource = null;
GridView1.DataBind();

call the above twice...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900