Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GridView, DataSource, RowUpdating, e.OldValues and e.NewValues collections

803 views
Skip to first unread message

Andrew Robinson

unread,
Mar 10, 2006, 12:06:03 AM3/10/06
to
A friend asked me to help him out on this and frankly it has me completely
stumped as well.

I have a GridView control and use old style manual binding with a DataSource
property and invoke the DataBind() method. If you don't use one of the Data
Source controls, you are required to handle the RowUpdating event. I
confirmed this using Reflector:

protected virtual void OnRowUpdating(GridViewUpdateEventArgs e)
{
bool flag1 = base.IsBoundUsingDataSourceID;
GridViewUpdateEventHandler handler1 = (GridViewUpdateEventHandler)
base.Events[GridView.EventRowUpdating];
if (handler1 != null)
{
handler1(this, e);
}
else if (!flag1 && !e.Cancel)
{
throw new HttpException(SR.GetString("GridView_UnhandledEvent",
new object[] { this.ID, "RowUpdating" }));
}
}

But, the collections associated with e.NewValues and e.OldValues passed as
an event argument are always empty. Is there any easy to retrieve these
values within the event? If not, the event seems of little value especially
with the control requires you to subscribe to it when not using a Data
Source control.

Yes, these values are passed when using an Object Data Source (and I would
assume a SQL Data Source as well.)


Thanks,


Steven Cheng[MSFT]

unread,
Mar 10, 2006, 5:23:47 AM3/10/06
to
Hi Andrew,

Welcome to the ASP.NET newsgroup.

Regarding on the GridView control's RowUpdating event problem, it is the
expected behavior because when we do not associate GridView(or other
ASP.NET 2.0 databound control) with DataSource control, it won't
automatically query and fill the parameters collection of the
updating/deleting/... events. In such cases, we need to manually extract
the field values from the Template control. Fortunately , ASP.NET 2.0
provide some good helper functions which ease our work on extracting field
values from template databound control's datafields. e.g. the
BoundField.ExtractValuesFromCell method:

#BoundField.ExtractValuesFromCell Method
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfiel
d.extractvaluesfromcell.aspx

So in our GridView's RowUpdating event, we can manually query the data
fields values (key , old values , new values...) from the corresponding
cells. For example:

=======================
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{

DataControlFieldCell cell = GridView1.Rows[e.RowIndex].Cells[0] as
DataControlFieldCell;

GridView1.Columns[0].ExtractValuesFromCell(
e.Keys,
cell,
DataControlRowState.Normal,
true);


cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;


GridView1.Columns[1].ExtractValuesFromCell(
e.NewValues,
cell,
DataControlRowState.Edit,
true);


foreach (string key in e.Keys.Keys)
{
Response.Write("<br/>" + key + ": " + e.Keys[key]);
}

foreach (string key in e.NewValues.Keys)
{
Response.Write("<br/>" + key + ": " + e.NewValues[key]);
}


...................................
}
============================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


laan_se_66

unread,
Apr 5, 2006, 2:37:02 PM4/5/06
to
This works fine if the column is visible but I use Visible="false". This was
as I understood it a bug in the beta but I'm using 2.0. Is there any work
around?

mravic...@gmail.com

unread,
Apr 19, 2006, 4:08:43 PM4/19/06
to
Stephen,

thanks for your posting. i modified the code but the _rowUpdating
function is not getting the new values but are still looking at the old
values. i was surprised that the person who posted the question above
does not face the same problem.

i had used the dataControlFieldCell and i had also used the e.NewValues
posted elsewhere. none of them worked.

thanks and regards
Ravi.

swta...@yahoo.com

unread,
May 3, 2006, 7:14:48 PM5/3/06
to
This does work if you use:

foreach (string value in e.Keys.Values)
{
Response.Write("<br/>Value: " + value);
}

Peter Kornills

unread,
May 25, 2006, 5:25:47 PM5/25/06
to
Hi there,

Sorry but that doesn't work either...

Here's my Code
--->
DataControlFieldCell cell = myDGV.Rows[e.RowIndex].Cells[6] as
DataControlFieldCell;
myDGV.Columns[6].ExtractValuesFromCell(e.Keys, cell,
DataControlRowState.Normal, true);
cell = myDGV.Rows[e.RowIndex].Cells[5] as DataControlFieldCell;
myDGV.Columns[5].ExtractValuesFromCell(e.NewValues, cell,
DataControlRowState.Edit, true);

//Output Try 1
foreach (string value in e.NewValues.Values)
{
Label3.Text = Label3.Text + ("<br/>T1Value: " + value);
}

//Output Try 2


foreach (string key in e.Keys.Keys)
{

Label3.Text=Label3.Text + ("<br/>" + key.ToString() + ": "
+ e.Keys[key].ToString());


}
foreach (string key in e.NewValues.Keys)
{

Label3.Text = Label3.Text + ("<br/>" + key + ": " +
e.NewValues[key]);
}
<---
I inserted the key-column, after I read this article (it's column[6]
//visible= false)
The values to verify are placed in column[5]
I always get the values which stand originaly before calling edit and
update in the gridview .

Can anyone help me please?

Thanx, Peter

someone

unread,
Jun 13, 2006, 12:10:00 PM6/13/06
to
Use the DataKeyNames property in the gridview and set it to the hidden
column you are trying to access.

*** Sent via Developersdex http://www.developersdex.com ***

arvind singh

unread,
Dec 5, 2006, 12:27:17 AM12/5/06
to

Hi,

In advancd thanx,

Can any one help me, I am new in ASP.NET 2.0. I am using GridView - when
i try to update the GridView its not working. I want update dynamically

Code:


protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs
e)
{
DataControlFieldCell cell = GridView1.Rows
[e.RowIndex].Cells[0] as DataControlFieldCell;

GridView1.Columns[0].ExtractValuesFromCell(
e.Keys,

cell,
DataControlRowState.Normal,
true);

cell = GridView1.Rows[e.RowIndex].Cells[1] as
DataControlFieldCell;

GridView1.Columns[1].ExtractValuesFromCell(
e.NewValues,
cell,
DataControlRowState.Edit,
true);

foreach (string key in e.Keys.Keys)
{
response.write("<br/>" + key + ": " + e.Keys[key]);
}

foreach (string key in e.NewValues.Keys)
{

response.write("<br/>" + key + ": " + e.NewValues[key]);
}

...................................
}

But this code return old value not new update(Edited value).

Help e to solve this problem.

serdar kacka

unread,
Jun 6, 2007, 11:49:04 AM6/6/07
to

Hi everybody,

Firstly I would like to thank you for your solution to this exhausting
problem.

I had a DataTable instance and would like to update its rows using a
gridview. In my PageLoad i got my table and put it in Session so in
gridview events i used this table for updating gridview. Here is my page
class:

//********
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{

DataTable table = GetTable();
Session["Table"] = table;
GridView1.DataSource = table;
GridView1.DataBind();
}

}

DataTable GetTable()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Surname");

DataRow row = dataTable.NewRow();
row[0] = "Franc";
row[1] = "Kafka";

dataTable.Rows.Add(row);

row = dataTable.NewRow();
row[0] = "Orhan";
row[1] = "Pamuk";

dataTable.Rows.Add(row);

return dataTable;
}


protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
DataTable table = Session["Table"] as DataTable;

GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = table;
GridView1.DataBind();
}

protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
DataTable table = Session["Table"] as DataTable;

GridView1.EditIndex = -1;
GridView1.DataSource = table;
GridView1.DataBind();
}


protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{

DataTable dataTable = Session["Table"] as DataTable;

for (int i = 0; i < dataTable.Columns.Count; i++)
{
DataControlFieldCell
cell = GridView1.Rows[e.RowIndex].Cells[i] as
DataControlFieldCell;
GridView1.Columns[i].ExtractValuesFromCell(e.NewValues,
cell, DataControlRowState.Edit, true);
}

foreach (string key in e.NewValues.Keys)
{
//Response.Write("<br/>" + key + ": " + e.NewValues[key]);
dataTable.Rows[e.RowIndex][key] = e.NewValues[key];
}

GridView1.EditIndex = -1;
Session["Table"] = dataTable;
GridView1.DataSource = dataTable;
GridView1.DataBind();
}

//********
And my code snippet from Default.aspx:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"
/>
<asp:BoundField DataField="Surname"
HeaderText="Surname" />
<asp:CommandField ButtonType="Button"
ShowEditButton="True" />
</Columns>
</asp:GridView>

//********

Thank you again.

Bhagya Jadhav

unread,
Jun 27, 2007, 6:18:44 AM6/27/07
to

Thanks a lot, I was getting very irratated with Gridview1_Updating event
from a long time...Thaks a lot, your code helped me solve my problems.

praveen kumar

unread,
Aug 6, 2007, 6:03:59 AM8/6/07
to

i need catching the new values in gridviw control.

Andrew Crawford

unread,
Sep 12, 2007, 12:31:14 PM9/12/07
to
Did you ever get this working? I have the exact same problem (using VB
instead of C#) where my return shows old data; not new: *You might just
be saving my life, as i'm about to jump out the window*

Dim cell As DataControlFieldCell
cell = Race_GV.Rows(e.RowIndex).Cells(1)
Race_GV.Columns(0).ExtractValuesFromCell(e.Keys, cell,
DataControlRowState.Normal, True)
cell = Race_GV.Rows(e.RowIndex).Cells(2)
Race_GV.Columns(1).ExtractValuesFromCell(e.NewValues, cell,
DataControlRowState.Edit, True)
Dim key As String
For Each key In e.Keys.Keys
Response.Write("<br/>old: " + key + ": " + e.Keys(key))
Next
For Each key In e.NewValues.Keys
Response.Write("<br/>new: " + key + ": " + e.NewValues(key))
Next

Christoph Khodja

unread,
Sep 14, 2007, 4:51:06 AM9/14/07
to
Hey Guys,

the underlying problem is a timing problem with the DataBind() Method.

Once you've clicked the update button of your list. The page will be
postbacked. Normally most of us use the Page_Load-Method to invoke
DataBind(). This causes the gridview to refresh databinding for the
boundcolumns.

So, afterwards you enter your updating method e. g.
GridView2_RowUpdating(...). At this point you've lost the game because
of data-re-binding :-)

Try to do it this way (see following code snippet)

Happy .NETing

Chrischi

namespace MyWebTest
{
public partial class WebForm1 : System.Web.UI.Page
{
MyWebTest.DataSet1TableAdapters.dbo_ProductsTableAdapter adp;
MyWebTest.DataSet1.dbo_ProductsDataTable dtb;

protected void Page_Load(object sender, EventArgs e)
{

if (this.IsPostBack)
{
dtb =
(MyWebTest.DataSet1.dbo_ProductsDataTable)this.Session["dtb"];
adp =
(MyWebTest.DataSet1TableAdapters.dbo_ProductsTableAdapter)this.Session["
adp"];
}
else
{
adp = new
MyWebTest.DataSet1TableAdapters.dbo_ProductsTableAdapter();
dtb = new DataSet1.dbo_ProductsDataTable();

dtb = adp.GetData();
this.Session.Add("dtb", dtb);
this.Session.Add("adp", adp);

BindGrid();
}

}

private void BindGrid()
{
GridView2.DataSource = dtb;
GridView2.DataBind();
}

protected void GridView2_RowEditing(object sender,
GridViewEditEventArgs e)
{
this.Session.Add("index", e.NewEditIndex);
GridView2.EditIndex = e.NewEditIndex;
String country =
GridView2.Rows[e.NewEditIndex].Cells[1].Text;
BindGrid();
}

protected void GridView2_RowUpdating(object sender,


GridViewUpdateEventArgs e)
{
DataControlFieldCell cell =

GridView2.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;

GridView2.Columns[0].ExtractValuesFromCell(e.Keys, cell,
DataControlRowState.Normal, true);

cell = GridView2.Rows[e.RowIndex].Cells[1] as
DataControlFieldCell;

GridView2.Columns[0].ExtractValuesFromCell(e.NewValues,
cell, DataControlRowState.Edit, false);

// Show something


foreach (string key in e.Keys.Keys)
{

this.Response.Write("<br/>" + key + ": " + e.Keys[key]);
}

foreach (string key in e.NewValues.Keys)
{

this.Response.Write("<br/>" + key + ": " +
e.NewValues[key]);
}

foreach (string key in e.NewValues.Keys)
{
dtb.Rows[e.RowIndex][key] = e.NewValues[key];
}

adp.Update(dtb);

San

unread,
Sep 28, 2007, 5:31:02 AM9/28/07
to
Thanks Christoph. I will try that.

After researching for so long for a solution to this problem, I was able to
do it without problem with datagrid. I think this is a BUG of gridview.

Will Microsoft provide a fix for that?

gv no more

unread,
Aug 4, 2009, 7:13:02 AM8/4/09
to
GridView is terrible!!

it's simply NOT FINISHED and was released by mistake!!

From http://www.developmentnow.com/g/12_2006_3_0_0_712737/GridView-DataSource-RowUpdating-e-OldValues-and-e-NewValues-collections.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

Phil

unread,
Feb 25, 2010, 7:38:04 AM2/25/10
to
Hello, i have the same problem. I am trying a new solution using ObjectDataSource for binding data with the Gridview. It seems to be the cleanest way for managing data in a custom gridview. With this method, it may be possible to get the right data in e/OldValues and e.NewValues.
0 new messages