EditItemIndex not working when adding new records with datagrid

Discussion in 'ASP .Net Web Controls' started by tafs7, Sep 11, 2003.

  1. tafs7

    tafs7 Guest

    I have a datagrid in which I added a linkbutton (to the footer of my
    only template column) that raises the OnClick event and fires the
    AddNew method (see code below). I'm trying to add a new record to the
    database by setting all its fields to a value of 0, then I need to get
    to the edit mode of the data grid, for that specific record and be
    able to edit its values from the initialized zeroes, to whatever
    they'll be.

    The event fires, and the new record is inserted into the database just
    fine. The problem is that the datagrid never goes into edit mode so
    that I can edit the newly added record. The user has to manually page
    to the new record (which is the last page of the grid), then click on
    the edit command from the EditCommandColumn.

    How can I automatically go into editmode after adding the new record?

    I know the edit mode will work if I am doing updates to an existing
    record, that's why I was inserting a new "blank" record to the db,
    then trying to edit that record. Please would somebody help. Is there
    a better way of doing this, the adding new records with datagrid,
    etc.?

    regards,
    Thiago Silva
    web developer
    AgniTEK

    Below is my code for the AddNew event and the datagrid's
    editcommandcolumn, plus the footertemplate that contains the
    LinkButton AddNew that raises the event:
    <asp:DataGrid id=dgClientInfo runat="server" ShowFooter="True"
    AutoGenerateColumns="False"
    GridLines="None"
    Cellpadding="5"
    BackColor="LightBlue"
    BorderColor="Navy"
    BorderStyle="Dashed"
    BorderWidth="1px"
    CssClass="setText"
    AllowPaging="True"
    OnPageIndexChanged="dgClientInfo_Page"
    PageSize="1"

    OnCancelCommand="dgClientInfo_Cancel"
    OnUpdateCommand="dgClientInfo_Update"
    OnDeleteCommand="dgClientInfo_Delete"
    OnEditCommand="dgClientInfo_Edit"

    HeaderStyle-BackColor="lightgrey"
    HeaderStyle-Font-Name="Verdana"
    HeaderStyle-Font-Bold="True"
    HeaderStyle-HorizontalAlign="Right"

    FooterStyle-BackColor="lightgrey"
    FooterStyle-Font-Name="Verdana"
    FooterStyle-Font-Bold="True" >
    <Columns>
    <asp:EditCommandColumn EditText=" Edit "
    CancelText=" Cancel "
    UpdateText=" Save "
    ItemStyle-Wrap="True"
    ItemStyle-Width="15"
    ItemStyle-VerticalAlign="Top"/>
    (...)

    </EditItemTemplate>
    <FooterTemplate>
    <asp:LinkButton Runat="server"
    Text="Add New Record"
    OnClick="AddNew"
    ID="LinkButton1"
    Name="LinkButton1" />
    | <asp:LinkButton Runat="server"
    Text="View All Records"
    OnClick = "ClearSearch"
    id="LinkButton2"
    name="LinkButton2" />
    </FooterTemplate>
    </asp:TemplateColumn>
    </Columns>

    (...)
    void AddNew(Object source, EventArgs e)
    {
    string s = "SELECT * FROM tblTAIP";
    DataSet newDS = GetDS(s);
    DataTable table = newDS.Tables[0];
    DataRow newrow = table.NewRow();
    foreach(DataColumn dc in table.Columns)
    {
    if(dc.DataType==System.Type.GetType("System.Int32"))
    newrow[dc] = 0;
    else
    newrow[dc] = "0";
    }

    table.Rows.Add(newrow);

    //Connection String to the database
    string strConn = ConfigurationSettings.AppSettings["DBConnectionString"];

    //sql statement to get the datagrid binding table
    string strDGSQL = "SELECT * FROM tblTAIP";

    SqlDataAdapter adapter = new SqlDataAdapter(strDGSQL, strConn);
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
    adapter.Update(table);

    dgClientInfo.DataSource = newDS;
    dgClientInfo.DataBind();

    //find the last record in the datagrid
    // which is the only one in the page (pagesize=1)

    dgClientInfo.EditItemIndex = dgClientInfo.PageCount-1;
    dgClientInfo.DataSource = newDS;
    dgClientInfo.DataBind();

    }
     
    tafs7, Sep 11, 2003
    #1
    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. karim
    Replies:
    0
    Views:
    338
    karim
    Jul 16, 2003
  2. AnandaSim

    Adding new records to a grid dynamically

    AnandaSim, Mar 1, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    300
    AnandaSim
    Mar 1, 2005
  3. Just Me

    Datagrid.EditItemIndex Puzzle

    Just Me, Aug 30, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    404
    Just Me
    Aug 30, 2007
  4. tafs7
    Replies:
    0
    Views:
    126
    tafs7
    Sep 11, 2003
  5. PaulNaude

    Datagrid: How do I allow edits but not new records?

    PaulNaude, Sep 19, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    117
    Brock Allen
    Sep 19, 2005
Loading...

Share This Page