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.
Related
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
}
}
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 have a gridview that deletes data from xml file when delete button on row is clicked. The problem is after I delete a row and refresh the page the rows below are automatically deleted on each page refresh even though their delete row button is not clicked
I don't know how to solve this
this is my delete function:
BindGridView();
DataSet dsGrid = (DataSet)BankGrid.DataSource;
dsGrid.Tables[0].Rows[BankGrid.Rows[e.RowIndex].DataItemIndex].Delete();
dsGrid.WriteXml(Server.MapPath("XMLFile.xml"));
BindGridView();
Your delete code should be sitting in the grid view row deleting event
DataSet dsGrid = banksDataSet.ReadXml(filePath); //global variable
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dsGrid.Tables[0].Rows[BankGrid.Rows[e.RowIndex].DataItemIndex].Delete();
dsGrid.AcceptChanges();
dsGrid.WriteXml(Server.MapPath("XMLFile.xml"));
BindGridView();
}
protected void BindGridView()
{
GridView1.Datasource=dsGrid;
Gridview1.DataBind();
}
And in your page load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView()
}
}
I have a gridview using object datasource for data binding. Everything is working fine except, When i add some new records to data it is not displaying immediately, it requires a refresh. I am using L2S Business Object with Object Data Source. Same thing in update and delete events.
I think you miss EditIndex property, change it on every event, like :
protected void HlnkbInsert_Click(object sender, EventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
...
gv.EditIndex = e.NewSelectedIndex;
DataBindGV();
}
Are you re-binding your GridView after making the changes to your data?
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
}