Problem understanding how to make a dynamic GridView execute Update on SqlDataSource

P

PeterKellner

I've create a very simple example that runs against the northwind
database that does not update the underlying database as I would
expect. I'm thinking there must be something I have to execute in
order to have the sqldatasource's Update actually run. The Updating
event does get called in the code below.

-thanks for any help on this. -Peter

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected GridView gv;

protected void Page_Load(object sender, EventArgs e)
{
gv = new GridView();

SqlDataSource sqlDataSource = new SqlDataSource();
sqlDataSource.ConnectionString = "Data Source=acer8200;Initial
Catalog=Northwind;Integrated Security=True";
sqlDataSource.SelectCommand = "SELECT [CategoryID],
[CategoryName] FROM [Categories]";

sqlDataSource.UpdateParameters.Clear();
sqlDataSource.UpdateParameters.Add("CategoryName",
TypeCode.String, string.Empty);
sqlDataSource.UpdateParameters.Add("CategoryI",
TypeCode.Int32, "0");

gv.DataSource = sqlDataSource;
gv.AutoGenerateEditButton = true;
gv.AutoGenerateDeleteButton = true;

gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
gv.RowUpdating += new
GridViewUpdateEventHandler(gv_RowUpdating);
gv.RowUpdated += new
GridViewUpdatedEventHandler(gv_RowUpdated);
gv.RowDeleting += new
GridViewDeleteEventHandler(gv_RowDeleting);
gv.RowDeleted += new
GridViewDeletedEventHandler(gv_RowDeleted);
gv.RowCancelingEdit += new
GridViewCancelEditEventHandler(gv_RowCancelingEdit);


form1.Controls.Add(gv);
gv.DataBind();
}

void gv_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
gv.DataBind();
}

void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
gv.EditIndex = -1;
gv.DataBind();
}

void gv_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
gv.DataBind();
}

void gv_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
gv.DataBind();
}

void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
gv.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Dynamic GridView Create With Edit</title>
</head>
<body>
<form id="form1" runat="server">

</form>
</body>
</html>
Peter Kellner
http://peterkellner.net
 
M

mou piya

Your article helped me a lot.Thanks for that.
I have one doubt.If i am using template fields in the gridview then
how to go?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top