Gridview row updating button not working - c#

My gridview row updating button is not working when I press edit and then press on the button to apply the changes it doesn't do anything. Am I missing something?
My code looks like this
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string afspraak = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditAfspraak"))).Text;
string uitleg = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditUitleg"))).Text;
int id = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditID"))).Text);
_controller.RecordUpdatenTblAfspraken(afspraak, uitleg, id);
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
}
ASP.NET
Couldn't paste it correctly so it's here:
https://pastebin.com/S3S09nLL
Highlighted button isn't working

You have to rebind the grid after you change the EditIndex.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = mijnDatabaseBron;
GridView1.DataBind();
}
The same goes for GridView1_RowCancelingEdit and when update has succeeded in GridView1_RowUpdating. This is assuming you added the OnRowEditing, OnRowCancelingEdit and OnRowUpdating events to the GridView.
Also as user7415073 commented, the binding of the GridView must be inside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = mijnDatabaseBron;
GridView1.DataBind();
}
}
And see my answer here as to how you can easily pass the EditID to code behind.
Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C#

Related

Deleting a row when the user presses delete button

This might be a easy problem but i don't see what i'm doing wrong here. The code is:
protected void dgvSelected_RowCommand (object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="Delete")
{
if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
{
int RowIndex = Convert.ToInt32(e.CommandArgument);
dgvSelected.DeleteRow(RowIndex);
dgvSelected.DataBind();
}
}
}
And at the end of the class i have this method.
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I'm using asp .net and i have a gridview that several rows and at each row i have a delete button where the user can press it to delete the row. For the moment when i press the delete button the whole gridview disappears. What am i doing wrong?
Try this
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dgvSelected.DataBind();
}
Since the gridview data was from antoher gridview, the solution was to make a viewstate variable and save the datatable in it. And then to delete one row:
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["SelectedValue"] as DataTable;
dt.Rows[index].Delete();
ViewState["SelectedValue"] = dt;
dgvSelected.DataSource = dt;
dgvSelected.DataBind();
}

Detailsview on ModalPopUp only displays first row of gridview

I have a detailsview inside a modalpopup that is supposed to be displaying the selected row in my gridivew, but it is only displaying the first row in my gridview. What am I doing incorrectly? Thank you for your help.
protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
this.dvstudent.Visible = true;
this.dvstudent.DataBind();
this.UpdatePanelDetailsView1.Update();
this.modalstudent.Show();
}
protected void Selecttomodifystudent_Click(object sender, EventArgs e)
{
LinkButton buttonstudent = sender as LinkButton;
GridViewRow row = (GridViewRow)buttonstudent.NamingContainer;
this.sqlModifyStudent1.SelectParameters.Clear();
this.dvstudent.DataBind();
this.UpdatePanelDetailsView1.Update();
this.modalstudent.Show();
}
I found the answer to my problem. It was right before my eyes:
protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
dvstudent.PageIndex = gvStudent.SelectedIndex;
}

Gridview won't rebind data after changing Gridview page

I am trying to populate my gridview with amazon search results. At the moment when the page loads, datasource is filled with data. What I am trying to do is show the data after pressing the Search button, but it displays "No Records Found". I have tried many different ways, the only way it worked, was without postback, but then the issue was that every time I changed the page on the gridview, GetProducts("Playstation") command was initiated over again.
The solution I have been searcing for: Load the page -> click button -> fill the gridview with data -> When choosing new page in gridview, data is displayed but Getproducts("Playstation") is not initiated again.
Is there a way to do this ?
protected void Page_Load(object sender, EventArgs e) {
Button1.Click += new EventHandler(this.GreetingBtn_Click);
if (!Page.IsPostBack) {
AmazonSearch us = new Amazon.PAAPI.AmazonSearch();
GridView1.DataSource = us.GetProducts("Playstation");
}
}
void Search(Object sender, EventArgs e) {
Button clickedButton = (Button) sender;
GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
EDIT
I figured it out thanks to FastGeeks anwser. I added variable ds to the code. and made the following changes:
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e) {
Button1.Click += new EventHandler(this.GreetingBtn_Click);
}
void Search(Object sender, EventArgs e) {
Button clickedButton = (Button) sender;
AmazonSearch us = new Amazon.PAAPI.AmazonSearch();
ds.Tables.Add(us.GetProducts("Playstation"));
GridView1.DataSource = ds;
Session["ds"] = ds;
GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
GridView1.PageIndex = e.NewPageIndex;
ds = (DataSet)Session["ds"];
GridView1.DataSource = ds;
GridView1.DataBind();;
}
You need to assign DataSource again in PageIndexChanging before binding it.
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = us.GetProducts("Playstation");
GridView1.DataBind();
}
Similarly assign DataSource in search method too.
void Search(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
GridView1.DataSource = us.GetProducts("Playstation");
GridView1.DataBind();
}
My take on this would be to store the results from the Amazon search into a DataTable, and then store the DataTable in a session variable; this is entirely possible because a DataTable is Serializable and will store in the session.
Then in your grid_PageIndexChanging event, and Search method you can retrieve the DataTable from session and re-assign your data without repeating the Amazon search.

Gridview paging not working

I have a GridView control which I fill thru c# code and want to do paging with code like this
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGrid();
GridView1.PageIndex = e.NewPageIndex;
GridView1.SelectedIndex = -1;
}
The BindGrid() function is where I get the data source for the grid and bind it.
It all works nicely exept that I have to press twice in order for it to change pages.
What can I do so that it will page after one click?
Thank you.
Your binding order is not correct.. It should be like...
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.SelectedIndex = -1;
BindGrid(); // Call bind here
}

Having to click twice in my edit gridview

OK So I have a web app with a dropdown field, a show button and a gridview that i can edit. Page loads, I choose my dropdown value, page loads fine. When I go to click the edit button however, I have to click it twice in order to be able to edit or cancel (having issues with that also but thats a different issue)
Anyway, I want to be able to have one click on edit to bring up the update/cancel editmode. I'm new to C# Web apps so some insight would be helpful.
Thanks
My ASP
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowEditing="TaskGridView_RowEditing"
OnRowCancelingEdit="TaskGridView_RowCancelingEdit"
OnRowUpdating="TaskGridView_RowUpdating" >
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
My C#
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
GridView1.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
// BindData();
}
protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
Image1.Visible = true;
Image2.Visible = false;
}
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
System.Data.DataTable dt = (System.Data.DataTable)Session["EditDataPage"];
//Update the values.
GridViewRow row = GridView1.Rows[e.RowIndex];
// dt.Rows[row.DataItemIndex]["QuoteNumber"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["ItemNumber"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
//dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
// dt.Rows[row.DataItemIndex]["Item"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["Descp"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["Route"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["Unit"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["IG"] = ((TextBox)(row.Cells[7].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["EXTQTY"] = ((TextBox)(row.Cells[8].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["CSTCD"] = ((TextBox)(row.Cells[9].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["PCOST"] = ((TextBox)(row.Cells[10].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["SCOST"] = ((TextBox)(row.Cells[11].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["ACOST"] = ((TextBox)(row.Cells[12].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["TCOST"] = ((TextBox)(row.Cells[13].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["ICOST"] = ((TextBox)(row.Cells[14].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["BIZCODE"] = ((TextBox)(row.Cells[16].Controls[0])).Text;
// dt.Rows[row.DataItemIndex]["DeleteItem"] = ((TextBox)(row.Cells[17].Controls[0])).Text;
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
private void BindData()
{
GridView1.DataSource = Session["Sqldatasource1"];
GridView1.DataBind();
in the rowediting event you do need to set editindex and then databind. you need to do it in the other events too. see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
your control may disappear if you databind unnecessarily. make sure you only databind in page_load once.
if(!Page.IsPostBack)
GridView1.DataBind();
My fix for having to click edit twice included ViewState.
My original Load and bind:
AttribGrid.DataSource = dataset;
AttribGrid.DataBind();
ViewState["CurTable"] = dataset;
then my subsequent RowEditing.
protected void AttribGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
AttribGrid.EditIndex = e.NewEditIndex;
AttribGrid.DataSource = (DataSet)ViewState["CurTable"];
AttribGrid.DataBind();
}
I did have to use EditIndex = newEditIndex. When I excluded it I still had to click twice. with the above code it was click once as expected.
You do not need to set the "GridView1.EditIndex" in the Editing or Canceling callback.
protected void My_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
// Perform a custom action in here
// But you don't need to set GridView1.EditIndex in here, that would be bad.
}

Categories