DataGrid Edit/Update problem

Discussion in 'ASP .Net' started by thebison, Apr 22, 2006.

  1. thebison

    thebison Guest

    Hi all,

    I hope someone can help with this relatively simple problem.
    I am building a timesheet application using ASP.NET C# with Visual
    Studio 2003.As it is only a protoype application, my database has been
    made in MSDE.

    My problem is to do with a DataGrid. I have successfully coded the
    DataGrid so that you can Edit, Update, Cancel. However as my Update
    Stored Procedure only updates certain columns I would like to make
    certain columns not appear as 'editable' when the user clicks on
    'Edit'. (Even though the stored procedure ignores any changes they
    make, I just want the option to edit removed to avoid confusion!...)

    My update method looks like this:

    private void Update_DataGrid(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    System.Web.UI.WebControls.TextBox cId = new
    System.Web.UI.WebControls.TextBox();
    System.Web.UI.WebControls.TextBox cName = new
    System.Web.UI.WebControls.TextBox();
    cId = (System.Web.UI.WebControls.TextBox) e.Item.Cells[0].Controls[0];
    cName = (System.Web.UI.WebControls.TextBox)
    e.Item.Cells[1].Controls[0];
    SqlCommand myCommand = new SqlCommand("NewUpdateDept",sqlConnection1);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.Add(new SqlParameter("@DeptID",SqlDbType.Int,4));
    myCommand.Parameters["@DeptID"].Value = cId.Text;
    myCommand.Parameters.Add(new
    SqlParameter("@DeptName",SqlDbType.VarChar,50));
    myCommand.Parameters["@DeptName"].Value = cName.Text;
    sqlConnection1.Open();

    followed by the usual try-catch......let me re-iterate that this works
    fine. I believe that I need to do something to make the e.Item.Cells
    part not visible on the cells I wish to exclude from the Edit mode.
    However I do not know what I need to do exactly?

    I hope this is clear enough, thanks in advance!

    Al
     
    thebison, Apr 22, 2006
    #1
    1. Advertising

  2. You can do something like this in the EditCommand event:-
    private void somedatagrid_EditCommand(object source,
    DataGridCommandEventArgs e)
    {
    Button b = new Button();
    b= (Button)e.Item.Cells[4].FindControl("What is the name of the
    control");
    b.Enabled = false;
    return;
    }

    Patrick

    "thebison" <> wrote in message
    news:...
    > Hi all,
    >
    > I hope someone can help with this relatively simple problem.
    > I am building a timesheet application using ASP.NET C# with Visual
    > Studio 2003.As it is only a protoype application, my database has been
    > made in MSDE.
    >
    > My problem is to do with a DataGrid. I have successfully coded the
    > DataGrid so that you can Edit, Update, Cancel. However as my Update
    > Stored Procedure only updates certain columns I would like to make
    > certain columns not appear as 'editable' when the user clicks on
    > 'Edit'. (Even though the stored procedure ignores any changes they
    > make, I just want the option to edit removed to avoid confusion!...)
    >
    > My update method looks like this:
    >
    > private void Update_DataGrid(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > System.Web.UI.WebControls.TextBox cId = new
    > System.Web.UI.WebControls.TextBox();
    > System.Web.UI.WebControls.TextBox cName = new
    > System.Web.UI.WebControls.TextBox();
    > cId = (System.Web.UI.WebControls.TextBox) e.Item.Cells[0].Controls[0];
    > cName = (System.Web.UI.WebControls.TextBox)
    > e.Item.Cells[1].Controls[0];
    > SqlCommand myCommand = new SqlCommand("NewUpdateDept",sqlConnection1);
    > myCommand.CommandType = CommandType.StoredProcedure;
    > myCommand.Parameters.Add(new SqlParameter("@DeptID",SqlDbType.Int,4));
    > myCommand.Parameters["@DeptID"].Value = cId.Text;
    > myCommand.Parameters.Add(new
    > SqlParameter("@DeptName",SqlDbType.VarChar,50));
    > myCommand.Parameters["@DeptName"].Value = cName.Text;
    > sqlConnection1.Open();
    >
    > followed by the usual try-catch......let me re-iterate that this works
    > fine. I believe that I need to do something to make the e.Item.Cells
    > part not visible on the cells I wish to exclude from the Edit mode.
    > However I do not know what I need to do exactly?
    >
    > I hope this is clear enough, thanks in advance!
    >
    > Al
    >
     
    Patrick.O.Ige, Apr 23, 2006
    #2
    1. Advertising

  3. thebison

    thebison Guest

    Hi,

    Thanks for your reply. I have solved the first part of the problem by
    making the relevant columns 'read only' in the DataGrid property
    builder. The problem now however is that my Stored Procedure is not
    working, as it references the Primary Key unique identifier column in
    the DataGrid. To explain further, the code reads as follows:

    private void Update_DataGrid(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    System.Web.UI.WebControls.TextBox tId = new
    System.Web.UI.WebControls.TextBox();
    System.Web.UI.WebControls.TextBox tHrs = new
    System.Web.UI.WebControls.TextBox();
    //This is the problem line!
    tId = (System.Web.UI.WebControls.TextBox) e.Item.Cells[4].Controls[0];
    tHrs = (System.Web.UI.WebControls.TextBox) e.Item.Cells[3].Controls[0];

    SqlCommand myCommand = new
    SqlCommand("NewUpdateTimeSheet",sqlConnection1);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.Add(new
    SqlParameter("@TimeSheetID",SqlDbType.Int,4));
    myCommand.Parameters["@TimeSheetID"].Value = tId.Text;
    myCommand.Parameters.Add(new SqlParameter("@Hrs",SqlDbType.Float,8));
    myCommand.Parameters["@Hrs"].Value = Convert.ToSingle (tHrs.Text);

    This code worked fine before I made TimeSheetID 'read only'. The reason
    I want it read only is because I do not want the user to be able to
    update it. However it needs to be included as it is the unique
    identifier that is used in the Stored Procedure 'WHERE' clause (WHERE
    TimesheetID = @TimesheetID). The reason it is not working is that
    whereas before the cell was editable, you could assign the value in the
    editable cell to the 'tId.Text', now this is not possible. What code
    would I use to tell the Update command to take the value from a normal
    DataGrid cell - not in edit mode basically?..

    Sorry if that seems unclear, I am basically looking for a way of
    referencing the TimeSheetID in the DataGrid when performing my update
    stored procedure.

    I tried

    myCommand.Parameters["@TimeSheetID"].Value =
    e.Item.Cells[4].Controls[0];

    however this returned the "System.ArgumentOutOfRangeException:
    Specified argument was out of the range of valid values. Parameter
    name: index" error message.

    Any suggestions on how to do this?

    Thanks

    Al
     
    thebison, Apr 23, 2006
    #3
  4. What you can do is
    place a label control in you aspx page (in an itemtemplate) and then
    databind it to your
    "TimeSheetID"
    <asp:Label ID="TimeSheetID" text='<%# DataBinder.Eval(Container.DataItem,
    "TimeSheetID") %>' Runat="server" BorderWidth="0">
    in your codebehind grab the label id as follows using TimeSheetID.text
    and assign it to your
    myCommand.Parameters["@TimeSheetID"].Value = TimeSheetID.Text
    Since you don't need the TimeSheetID label in your aspx just set it to
    visible=false
    Hope that helps
    Patrick

    "thebison" <> wrote in message
    news:...
    > Hi,
    >
    > Thanks for your reply. I have solved the first part of the problem by
    > making the relevant columns 'read only' in the DataGrid property
    > builder. The problem now however is that my Stored Procedure is not
    > working, as it references the Primary Key unique identifier column in
    > the DataGrid. To explain further, the code reads as follows:
    >
    > private void Update_DataGrid(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > System.Web.UI.WebControls.TextBox tId = new
    > System.Web.UI.WebControls.TextBox();
    > System.Web.UI.WebControls.TextBox tHrs = new
    > System.Web.UI.WebControls.TextBox();
    > //This is the problem line!
    > tId = (System.Web.UI.WebControls.TextBox) e.Item.Cells[4].Controls[0];
    > tHrs = (System.Web.UI.WebControls.TextBox) e.Item.Cells[3].Controls[0];
    >
    > SqlCommand myCommand = new
    > SqlCommand("NewUpdateTimeSheet",sqlConnection1);
    > myCommand.CommandType = CommandType.StoredProcedure;
    > myCommand.Parameters.Add(new
    > SqlParameter("@TimeSheetID",SqlDbType.Int,4));
    > myCommand.Parameters["@TimeSheetID"].Value = tId.Text;
    > myCommand.Parameters.Add(new SqlParameter("@Hrs",SqlDbType.Float,8));
    > myCommand.Parameters["@Hrs"].Value = Convert.ToSingle (tHrs.Text);
    >
    > This code worked fine before I made TimeSheetID 'read only'. The reason
    > I want it read only is because I do not want the user to be able to
    > update it. However it needs to be included as it is the unique
    > identifier that is used in the Stored Procedure 'WHERE' clause (WHERE
    > TimesheetID = @TimesheetID). The reason it is not working is that
    > whereas before the cell was editable, you could assign the value in the
    > editable cell to the 'tId.Text', now this is not possible. What code
    > would I use to tell the Update command to take the value from a normal
    > DataGrid cell - not in edit mode basically?..
    >
    > Sorry if that seems unclear, I am basically looking for a way of
    > referencing the TimeSheetID in the DataGrid when performing my update
    > stored procedure.
    >
    > I tried
    >
    > myCommand.Parameters["@TimeSheetID"].Value =
    > e.Item.Cells[4].Controls[0];
    >
    > however this returned the "System.ArgumentOutOfRangeException:
    > Specified argument was out of the range of valid values. Parameter
    > name: index" error message.
    >
    > Any suggestions on how to do this?
    >
    > Thanks
    >
    > Al
    >
     
    Patrick.O.Ige, Apr 25, 2006
    #4
    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. Hartmut Schroth
    Replies:
    3
    Views:
    3,505
    Hartmut Schroth
    Dec 1, 2003
  2. Schultz
    Replies:
    3
    Views:
    567
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. Andreas Klemt
    Replies:
    0
    Views:
    309
    Andreas Klemt
    Oct 8, 2003
  4. Kostia
    Replies:
    1
    Views:
    267
    Scott Mitchell [MVP]
    Feb 26, 2004
  5. Replies:
    1
    Views:
    336
Loading...

Share This Page