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

Discussion in 'ASP .Net Datagrid Control' started by PeterKellner, May 20, 2006.

  1. PeterKellner

    PeterKellner Guest

    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
    PeterKellner, May 20, 2006
    #1
    1. Advertising

  2. PeterKellner

    mou piya Guest

    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?




    *** Sent via Developersdex http://www.developersdex.com ***
    mou piya, Jun 2, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. OceanTragic
    Replies:
    0
    Views:
    1,765
    OceanTragic
    Nov 14, 2006
  2. Will
    Replies:
    0
    Views:
    762
  3. Luqman
    Replies:
    5
    Views:
    1,407
    luqman
    Nov 22, 2007
  4. PeterKellner

    Problem understanding how to create a dynamic GridView in C#

    PeterKellner, May 19, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    407
    PeterKellner
    May 19, 2006
  5. Vaughn Haybittle
    Replies:
    0
    Views:
    881
    Vaughn Haybittle
    Apr 2, 2007
Loading...

Share This Page