I have a Gridview that is populated through SQLDataSource. The query behind is rather complex and the GridView takes some seconds to get filled; that's why I get annoyed by the fact that every time I select a row, the Gridview disappears for a while and is getting repopulated again. What does fire that rebind?
The selected row index works as Control Parameter for a second Gridview, that displays detail information on that row. There are these 2 events defined for the gridview:
protected void GridView_PURCHTABLE_OnDataBound(object sender, EventArgs e) {
if(DisplayPurchItems.Checked == false)
{
GridView_PURCHTABLE.Columns[4].Visible = false;
}
else
{
GridView_PURCHTABLE.Columns[4].Visible = true;
}
protected void GridView_PURCHTABLE_Selectedindexchanged(Object sender, EventArgs e) {
GridView_Notes.DataBind(); //this is the second gridview
}
Anyone has a clue what might cause the gridview to rebind?
Martin
Check once:Is post back.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//binding grid
}
}
Related
I've a condition in a gridview where I've to clear in the gridview selection when one of the selectIndexChanging event happens
void GridView1_SelectedIndexChanging(Object sender, EventArgs e)
{
// Some logic
//some condition
{
Gridview1.SelectedIndex = -1;
}
}
But the index is not getting cleared in this case in that given condition. Any suggestions on why this might not be working ? Thanks
It should be the SelectedIndexChanged event, not the SelectedIndexChanging event.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.DataSource = mySource;
GridView1.SelectedIndex = -1;
GridView1.DataBind();
}
If you want to cancel selection, then just set
e.Cancel=true;
So the pervious selection will remain.
See https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewselecteventargs(v=vs.110).aspx
I am searching for some data from a database into a datagrid. When I applied paging on it, on clicking the next link of paging the datagrid vanishes without showing anything. I used datagrid.databind() in pageload too when it's postback or I even made a postback event method named datagridname_onpageindexchanged() as :
protected void DataGridSearchResults_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGridSearchResults.currentpageindex=e.newpageindex;
DataGridSearchResults.databind();
}
Your aspx should include Paging :-
<asp:DataGrid ID="DataGridSearchResults" runat="server" AllowPaging="true" PageSize="10"
OnPageIndexChanged="DataGridSearchResults_PageIndexChanged">
If grid is having data at pageload, it should bind like this :-
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind your grid here.
}
}
Then the page index changed function :-
protected void DataGridSearchResults_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGridSearchResults.CurrentPageIndex = e.NewPageIndex;
//Bind your Grid here again.
}
If(! IsPostBack)
{
// Bind your dataGrid
}
see this link http://msdn.microsoft.com/enus/library/system.web.ui.webcontrols.datagrid.allowcustompaging.aspx
You need to set DataSource here for your datagrid.
protected void DataGridSearchResults_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGridSearchResults.CurrentPageIndex = e.NewPageIndex;
DataGridSearchResults.DataSource = YourDataSource;
DataGridSearchResults.DataBind();
}
Also ,If you are binding your grid in Page_Load, Make sure you are not binding your grid outside of if(!IsPostBack){}. Otherwise you will loose data on each postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Bind Your Grid Here
}
}
I am using GridView Row command Event for deleting row from Gridview, I have added RowDeleting Event of gridview,
but after deleting the GridView is getting hide.
Below is my code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack){
LoadData()//Here I am databinding the Grid
}
}
private void LoadData()
{
var data=MyClass.GetRecords();//it returns DataTable
dg.DataSource=data;
dg.DataBind();
}
protected void dg_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="del")
{
//Delete Records
dg.DataBind();
}
}
public void dg_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
dg.DataBind();
}
My guess is you are missing DataSource after deleting the rows.
Try this
protected void dg_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="del")
{
//Delete Records
LoadData();
}
}
public void dg_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
LoadData();
}
The GridView does not show any records because you do not assign a DataSource to it before calling DataBind in dg_RowDeleting.
You need to reassign the DataSource before calling DataBind anew, because it is not set again on a PostBack.
So a typical approach for the Delete part of your dg_RowCommand method would be:
Identify the row that is to be deleted. Remove the row from the database.
Reload the data from the database.
Assign the data to the DataSource property.
Bind the GridView by calling DataBind().
For steps 2-4 you will be able to call your LoadData method from dg_RowCommand. You only need to implement dg_RowDeleting if you do not implement the deletion yourself but want to do something when a row is deleted.
I have two files in my project. One is user control (popup) customerpicker.ascx and one is default.aspx page. In customerpicker I have dynamically generated gridview and 'select' column with SelectButton.
What I want is this: When I click on 'select' on random row in gridview, then I like to display value from selected row immediately (like ajax) to aspx.page. How it is possible?
There is part of my code in .ascx:
public string showOnaspx { get; set; }
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
showOnaspx = row.Cells[1].Text;
e.Cancel = true;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
}
I tried casting, get/set methods, but nothing worked for me.
Answer on that question is pretty easy.. We can save variabile in session and then read it in aspx.page wherever we want.
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Session["sessionid"]= row.Cells[1].Text;
e.Cancel = true;
}
Feeling so stupid right now, because I spent few days to figure it out simple way to do that.
I had gridview which retrieve Products from database by sqldatasource1 ,and my manager asked me to filter this gridview by DDL to filter gridview with specfic Model ,I add some function on gridview as edit,paging .I did my code well and gridview filtred by the Model_Id which come from DDL .But when I tried to edit any product or navigate through paging I faced this error (The GridView 'GridView1' fired event PageIndexChanging which wasn't handled. )when paging ,And this for editing (The GridView 'GridView1' fired event RowEditing which wasn't handled.)
So please any one help me.
(CS)
protected void Page_Load(object sender, EventArgs e)
{
BindGridFunction();
}
private void BindGridFunction()
{
if (DDLModel.SelectedIndex < 0)
{
GridView1.DataSource = SDSModel;
GridView1.DataBind();
}
else
{
GridView1.DataSource = SDSModel2;
GridView1.DataBind();
}
}
You need to explicitly handle PageIndexChanging() and RowEditing() event through code behind.
like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
public void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
{
//do your code here
}