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
  #11  
Old   
Christoph Khodja
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 09-14-2007 , 04:51 AM






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_ProductsTableA dapter adp;
MyWebTest.DataSet1.dbo_ProductsDataTable dtb;

protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
dtb =
(MyWebTest.DataSet1.dbo_ProductsDataTable)this.Ses sion["dtb"];
adp =
(MyWebTest.DataSet1TableAdapters.dbo_ProductsTable Adapter)this.Session["
adp"];
}
else
{
adp = new
MyWebTest.DataSet1TableAdapters.dbo_ProductsTableA dapter();
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);

}


}
}

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

Reply With Quote
  #12  
Old   
San
 
Posts: n/a

Default Re: GridView, DataSource, RowUpdating, e.OldValues and e.NewValues - 09-28-2007 , 05:31 AM






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?



"Christoph Khodja" wrote:

Quote:
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_ProductsTableA dapter adp;
MyWebTest.DataSet1.dbo_ProductsDataTable dtb;

protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
dtb =
(MyWebTest.DataSet1.dbo_ProductsDataTable)this.Ses sion["dtb"];
adp =
(MyWebTest.DataSet1TableAdapters.dbo_ProductsTable Adapter)this.Session["
adp"];
}
else
{
adp = new
MyWebTest.DataSet1TableAdapters.dbo_ProductsTableA dapter();
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);

}


}
}

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