Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > asp.net datagrid control > may 2006 >

asp.net datagrid control : Problem understanding how to make a dynamic GridView execute Update on SqlDataSource


PeterKellner
5/20/2006 7:20:58 AM
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
mou piya
6/2/2006 4:59:11 AM
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?




AddThis Social Bookmark Button