HighTechTalks DotNet Forums  

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

ASP.net Data Grid Control microsoft.public.dotnet.framework.aspnet.datagridcontrol


Discuss GridView, DataSource, RowUpdating, e.OldValues and e.NewValues collections in the ASP.net Data Grid Control forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Andrew Robinson
 
Posts: n/a

Default GridView, DataSource, RowUpdating, e.OldValues and e.NewValues collections - 03-10-2006 , 12:06 AM






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_UnhandledEven t",
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,



Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default RE: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues collections - 03-10-2006 , 05:23 AM






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/lib...rols.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.)







Reply With Quote
  #3  
Old   
laan_se_66
 
Posts: n/a

Default RE: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 04-05-2006 , 02:37 PM



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?

"Steven Cheng[MSFT]" wrote:

Quote:
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/lib...rols.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.)








Reply With Quote
  #4  
Old   
Peter Kornills
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 05-25-2006 , 05:25 PM



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


Reply With Quote
  #5  
Old   
someone
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 06-13-2006 , 12:10 PM



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

Reply With Quote
  #6  
Old   
arvind singh
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 12-05-2006 , 12:27 AM





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.

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

Reply With Quote
  #7  
Old   
serdar kacka
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 06-06-2007 , 11:49 AM





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.



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

Reply With Quote
  #8  
Old   
Bhagya Jadhav
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 06-27-2007 , 06:18 AM




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.


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

Reply With Quote
  #9  
Old   
praveen kumar
 
Posts: n/a

Default RE: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues collections - 08-06-2007 , 06:03 AM





i need catching the new values in gridviw control.



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

Reply With Quote
  #10  
Old   
Andrew Crawford
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 09-12-2007 , 12:31 PM



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.NewValu es, 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



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

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.