By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

DataGrid Edit/Update problem

P: n/a
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

Apr 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <al************@btinternet.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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

Apr 23 '06 #2

P: n/a
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

Apr 23 '06 #3

P: n/a
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" <al************@btinternet.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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

Apr 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.