DataGrid: OnUpdateCommand - getting the updated values

Discussion in 'ASP .Net Datagrid Control' started by Dan, Aug 18, 2005.

  1. Dan

    Dan Guest

    Greeting all,

    I have been reading a number of posts and all the samples I have seen
    are the same, yet I can't access the updated values of a TabelRow via:

    (TextBox)e.Item.Cells.Controls[0]

    I get a index out of bound error as there seems (from degbugging) to be
    no controls on *any* of the cells???

    I moved away from the traditional method of using asp:EditCommandColumn
    as the update event wasn't invoking the associated updated method and
    now use:

    <Columns>
    <asp:ButtonColumn Text="Edit" ButtonType="LinkButton"
    CommandName="Edit"></asp:ButtonColumn>
    <asp:ButtonColumn Visible=False Text="Update" ButtonType="LinkButton"
    CommandName="Update"></asp:ButtonColumn>
    <asp:ButtonColumn Visible=False Text="Cancel" ButtonType="LinkButton"
    CommandName="Cancel"></asp:ButtonColumn>
    </Columns>

    Which do fire the correct methods. Have I missed something? A snippet
    from the C# code is:

    public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs
    e)
    {
    SqlCommand cmd = new SqlCommand("usp_update_server", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    String[] cols = {"@srv_id", "@srv_name", "@sql_instance",
    "@nt_hostname", "@dt_purchased", "@directorate"};

    for (int i=4; i < e.Item.Cells.Count; i++) //skip the first 3
    columns (edit/update/cancel) and the 4th column which is the key
    {
    int offset = 3;

    String colvalue = (e.Item.Cells).Text;
    int x = e.Item.Cells.Controls.Count;
    System.Web.UI.WebControls.TableCell t = e.Item.Cells;

    try
    {
    TextBox y = (TextBox)e.Item.Cells.Controls[0];
    }
    catch
    {
    ;
    }

    if (cols[i-offset] == "@dt_purchased" && colvalue == "")
    cmd.Parameters.Add(new
    SqlParameter(cols[i-offset],DateTime.Today.ToString()));
    else if (cols[i-offset] == "@dt_purchased")
    cmd.Parameters.Add(new
    SqlParameter(cols[i-offset],DateTime.Parse(colvalue)));
    else
    cmd.Parameters.Add(new SqlParameter(cols[i-offset],colvalue));
    }

    and aas you can see, I've checked all the cells for the controls, but
    nothing!

    The ASP is:

    <form runat="server" name="currentServers" id="currentServers">
    <asp:datagrid id="MyDataGrid" runat="server"
    Width="700px"
    BackColor="#CCCCFF"
    BorderColor="Black"
    CellPadding="3"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="False"
    OnUpdateCommand="MyDataGrid_Update"
    OnEditCommand="MyDataGrid_Edit"
    OnCancelCommand="MyDataGrid_Cancel"
    DataKeyField="srv_id"
    Font-Names="Verdana">
    <Columns>
    <asp:ButtonColumn Text="Edit" ButtonType="LinkButton"
    CommandName="Edit"></asp:ButtonColumn>
    <asp:ButtonColumn Visible=False Text="Update"
    ButtonType="LinkButton" CommandName="Update"></asp:ButtonColumn>
    <asp:ButtonColumn Visible=False Text="Cancel"
    ButtonType="LinkButton" CommandName="Cancel"></asp:ButtonColumn>
    </Columns>
    </asp:datagrid>
    </form>

    Thanks in advance for any help offered!
    Regards,
    Dan
    Dan, Aug 18, 2005
    #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. =?Utf-8?B?S2FyaW5lIFByb290?=

    DataGrid : dynamic BoundColumn & OnUpdateCommand

    =?Utf-8?B?S2FyaW5lIFByb290?=, Jan 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,392
    =?Utf-8?B?S2FyaW5lIFByb290?=
    Jan 27, 2004
  2. Jim in Arizona
    Replies:
    0
    Views:
    374
    Jim in Arizona
    Dec 28, 2006
  3. DC
    Replies:
    2
    Views:
    612
  4. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    411
    Dmitry Korolyov
    Sep 22, 2003
  5. Jim in Arizona

    datalist vbCrLf & OnEditCommand/OnUpdateCommand Event Procedures

    Jim in Arizona, Dec 29, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    643
    Jim in Arizona
    Dec 29, 2006
Loading...

Share This Page