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,
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.)
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.
foreach (string value in e.Keys.Values)
{
Response.Write("<br/>Value: " + value);
}
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
*** Sent via Developersdex http://www.developersdex.com ***
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.
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.
i need catching the new values in gridviw control.
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
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);
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?
it's simply NOT FINISHED and was released by mistake!!
Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/