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()
}
}
Related
I have a gridview I want that when I click on "edit" button in gridview then edit row values inserted in the controls which is outside the gridview and then row is deleted. Can I call "delete" event method inside the "edit" event, because I want to first retrieve values in controls and then row is deleted.
Here is my aspx.cs code. Both delete and edit event code here :
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
t.Rows.RemoveAt(e.RowIndex);
GridView2.DataSource = t;
GridView2.DataBind();
}
public void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
TextBox1.Text=GridView2.Rows[e.NewEditIndex].Cells[1].Text.ToString();
DropDownList1.Text = GridView2.Rows[e.NewEditIndex].Cells[2].Text.ToString();
GridView2.EditIndex = -1;
GridView2.DataSource = t;
GridView2.DataBind();
//CAN I CALL GRIDVIEW_ROW_DELETING method here? I try but problem is arguments etc
}
As Emanuaele said, there's better ways to do this, but if you want to keep your code close to what you have, move your delete code into a separate method
protected void DeletingRow(int rowIndex)
{
DataTable t = (DataTable)Session["MyDataTable"];
t.Rows.RemoveAt(rowIndex);
GridView2.DataSource = t;
GridView2.DataBind();
}
Then GridView2_RowDeleting would change to
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DeletingRow(e.RowIndex);
}
And GridView2_RowEditing would change to
public void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
DaTable t = (DataTable)Session["MyDataTable"];
TextBox1.Text=GridView2.Rows[e.NewEditIndex].Cells[1].Text.ToString();
DropDownList1.Text = GridView2.Rows[e.NewEditIndex].Cells[2].Text.ToString();
GridView2.EditIndex = -1;
GridView2.DataSource = t;
GridView2.DataBind();
//CAN I CALL HERE GRIDVIEW_ROW_DELETING METHOD,I try but problem is arguemnts etc
DeletingRow(e.NewEditIndex);
}
Approach is wrong if data comes from a database.
You have to work on data, not on controls of page (you can but it's better not to do).
Don't use Session to store Datatable (how many records are there in?)
Use CommandArgument of your button to get the Id of the record.
Retrieve record from database and store fields in controls. Click a button, save data, delete the record and rebind.
Using your approach, you can move the code of deleting event in a private method and call it from editing event
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 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'm trying to change pageindex on page_Load event but its not working.
I can change page by clicking links in the page after page loaded.
If this info necessary GridView in UpdatePanel.
protected void Page_Load(object sender, EventArgs e)
{
/*...Some Codes...*/
//I'm trying to change page like this.
GridView1.PageIndex = Index;
GridViewPageEventArgs ea = new GridViewPageEventArgs(GridView1.PageIndex);
GridView1_PageIndexChanging(sender, ea);
}
protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
I believe that you need to rebind the data to your gridview after you change the page index.
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
}