RowUpdating GridView Showing Old value - c#

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
Row editing and Row canceling editing working fine; but when I press update after changing the value in textbox, it show the old value not that value that I have change.The following is Row updating event code.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
dao.AridNumber = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
dao.FirstName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
dao.LastName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
}
update me!

I think you need to bind the data again at the last of the method inorder to show you the appropriate result.
Hope this Helps!!

Check your markup that you're using Bind() as opposed to Eva() to bind the value to the TextBox.
Here is an example:
<EditItemTemplate>
<asp:TextBox ID="someId" runat="server" Text='<%# Bind("SomeField") %>'></asp:TextBox>
</EditItemTemplate>

I have recently came across the same problem you have faced , when i tried to update the value in grid. I have called the Gridbind in Pageload that's the cause of the issue.
Are you using any function to bind the gridview or something like that?
and if you are using that function in page_load event then you wont get the updated value cause on postback it rebinds ur gridview and you will lose ur updated value.

You need to call GridView1.DataBind() in the end:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
dao.AridNumber = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
dao.FirstName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
dao.LastName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
GridView1.DataBind();
}
Edit:
You might want to check out the RowUpdating documentation, especially I think that OldValues and NewValues properties might be of use to you.

Related

Why is my gridview rebound after selected index change?

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
}
}

Paging not working in Datagrid?

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
}
}

paging gridview rebinding for each result

i have a gridview with paging featuer,when i want to search item in DB and result displayed in gridview with paging
but when i jump another page gridview will Bind and list all items in DB with all pages,how can i solve this problem?
thanks in your advise
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
using (NoavaranModel.NoavaranEntities1 dbContext = new NoavaranModel.NoavaranEntities1())
{
var query = from list in dbContext.Students
select list;
lblStudentsCount.Text = query.Count().ToString();
GridView1.DataSource = query;
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
GridView1.DataBind();
}
If I'm understanding correctly, you're calling GridView1.DataBind() at the end of your BindGridView() method, so I don't believe you need to do it again after your call for BindGridView(); in the PageIndexChanging event:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
If this didn't solve it, please let me know what happens in the comments and I'll take another look at it.
Also, if you're saying that your GridView is filling up with all items from the database (when you're expecting only certain items), that indicates a problem with your query. I would try setting a breakpoint and step through the query to see what is coming back, just to make sure you're getting back your expected results.

error paging and editing in gridview

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
}

gridview_paging messing up

I have an asp.net C# web app. In it I have a gridview. The gridview gets search results from a database. Sometimes there are a lot of results, and so I wanted to use paging. Here's what I tried:
protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdResults.PageIndex = e.NewPageIndex;
grdResults.DataBind();
}
For some reason, when I click on a page number, it shows me the EmptyDataText(There are no records to display). What code would work?
Please help.
Thank you
Try assigning the datasource in NeedDataSource event.
Cheers.
You need to reassign your datasource to grdResults before the call to DataBind().
Try this code It will absoloutly work :
protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GV.PageIndex = e.NewPageIndex;
BindGrid();
}
public override void BindGrid()
{
query = new CommonQueries();
GV.DataSource = query.getAllBooks();
GV.DataBind();
}
the problem with your code is that you did'nt reassign the data source to your gridview !

Categories