I have another version of the project with object data source, which works ok, but I really need to update more that one table at a time,
that is why I am trying to using the row updating and programmatic binding. Do you know of a way to update more that one table with object data source; I didn't think it was possible? I will eventually have item template columns in gridview which call from
a second table adapter, although can't say it's going well so far...
It appears that the values are empty, but I acnnot figure out why. Thanks for replying.
Get the values from the controls in the GridView then:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int id = Convert.ToInt32(row.Cells[0].Text); // if id is a readonly field in your GridView
string name = ((TextBox)(row.Cells[1].Controls[0])).Text;
string desc = ((TextBox)(row.Cells[2].Controls[0])).Text;
productTableAdapter pAdapter = new productTableAdapter();
pAdapter.Update(id, name, description);
GridView1.EditIndex = -1;
GridView1.DataBind();
}
Plz set "DataKeyNames" property for the GridView。And then generate CRUD methods for the TableAdapter and bind it with ObjectDataSource to the GridView。
liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 11, 2012 01:39 AM|LINK
Thanks Decker for replying,
I have another version of the project with object data source, which works ok, but I really need to update more that one table at a time,
that is why I am trying to using the row updating and programmatic binding. Do you know of a way to update more that one table with object data source; I didn't think it was possible? I will eventually have item template columns in gridview which call from a second table adapter, although can't say it's going well so far...
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: TableAdapter Update GridView
Apr 11, 2012 01:50 AM|LINK
Just use Stored Procudre instead of a pure SQL statement or use something like Trigger to update related tables at a time。
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 11, 2012 07:19 AM|LINK
Get the values from the controls in the GridView then:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; int id = Convert.ToInt32(row.Cells[0].Text); // if id is a readonly field in your GridView string name = ((TextBox)(row.Cells[1].Controls[0])).Text; string desc = ((TextBox)(row.Cells[2].Controls[0])).Text; productTableAdapter pAdapter = new productTableAdapter(); pAdapter.Update(id, name, description); GridView1.EditIndex = -1; GridView1.DataBind(); }liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 12, 2012 12:50 AM|LINK
thanks, still get Input string was not in a correct format for any line when clicking update
Id is read only, primary key, autoincrement, still seems to be coming up NULL; there is data there
rechecked all data types...match in database, table adapter, code behind...
still trying things to no avail
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: TableAdapter Update GridView
Apr 12, 2012 01:00 AM|LINK
Hello liza99:)
Plz set "DataKeyNames" property for the GridView。And then generate CRUD methods for the TableAdapter and bind it with ObjectDataSource to the GridView。
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 12, 2012 08:02 AM|LINK
Is the id shown in the FIRST column of your GridView?
liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 12, 2012 12:38 PM|LINK
yes, id is in the first column. I've also tried moving it around, but no good results
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 12, 2012 12:41 PM|LINK
What's the value of row.Cells[0].Text and row.Cells[1].Controls.Count inside the RowUpdating event? Debug in Visual Studio to find out.