DataGrid easy Edit-Update using ADO.NET/DataGrid functionality C#?

Discussion in 'ASP .Net Datagrid Control' started by Kostia, Feb 26, 2004.

  1. Kostia

    Kostia Guest

    Hi, so I am using built in Edit, Delete, Update i.e.
    OnEditCommand="DataGrid1_Edit" OnUpdateCommand="DataGrid1_Update" with
    handler
    question below in regards to the actual update after edit

    //called by the handler deletes row no problem and updates DB no
    problem
    private void DeleteItem(DataGridCommandEventArgs e)
    {
    DataRow dr = dsEmployees1.Tables["Employees"].Rows[e.Item.ItemIndex];
    dr.Delete();
    sqlDataAdapter1.Update(dsEmployees1, "Employees");
    DataGrid1.EditItemIndex = -1;
    DataGrid1.DataBind();
    }

    public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs E)
    {
    DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
    DataGrid1.DataBind();
    }

    //NOW WHAT DO I NEED TO DO HERE TO ACTUALLY UPDATE THE ROW?
    //IS THE ONLY WAY TO DO IT IS by getting individual column values and
    //constructing SQL statement? IS there a way to call some kind of
    update to accept and commit entered changes? done through Edit?
    public void DataGrid1_Update(Object sender, DataGridCommandEventArgs
    E)
    {
    DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
    DataRow dr = dsEmployees1.Tables["Employees"].Rows[DataGrid1.EditItemIndex];
    sqlDataAdapter1.Update(dsEmployees1, "Employees");
    // Rebind the data source to refresh the DataGrid control.
    DataGrid1.EditItemIndex = -1;
    DataGrid1.DataBind();
    }
     
    Kostia, Feb 26, 2004
    #1
    1. Advertising

  2. > //NOW WHAT DO I NEED TO DO HERE TO ACTUALLY UPDATE THE ROW?
    > //IS THE ONLY WAY TO DO IT IS by getting individual column values and
    > //constructing SQL statement?



    Yes. This is precisely what you have to do. For a look at an article
    that builds an editable DataGrid, see:

    An Extensive Examination of the DataGrid Web Control: Part 6
    http://aspnet.4guysfromrolla.com/articles/071002-1.aspx

    Happy Programming!

    --

    <shameless plug>
    For more information on the DataGrid, DataList, and Repeater controls,
    consider picking up a copy of my latest book:

    ASP.NET Data Web Controls
    http://www.amazon.com/exec/obidos/ASIN/0672325012/4guysfromrollaco
    </shameless plug>

    Happy Programming!

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Feb 26, 2004
    #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. nita
    Replies:
    1
    Views:
    879
    Saravana
    Nov 20, 2004
  2. ronaldlee

    Transfer ADO Code to ADO.NET

    ronaldlee, Dec 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    465
    Kevin Spencer
    Dec 17, 2004
  3. Replies:
    0
    Views:
    1,320
  4. Elton Wang

    Datagrid Edit, Insert & Update Checkbox (ASP.NET Using VB.NET)

    Elton Wang, Apr 7, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    807
    Elton Wang
    Apr 7, 2005
  5. Replies:
    1
    Views:
    336
Loading...

Share This Page