DataGrid Edit/Update problem

T

thebison

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
 
P

Patrick.O.Ige

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
 
T

thebison

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
 
P

Patrick.O.Ige

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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top