Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataGrid Edit/Update problem

Reply
Thread Tools

DataGrid Edit/Update problem

 
 
thebison
Guest
Posts: n/a
 
      04-22-2006
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

 
Reply With Quote
 
 
 
 
Patrick.O.Ige
Guest
Posts: n/a
 
      04-23-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
 
 
 
thebison
Guest
Posts: n/a
 
      04-23-2006
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,);
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

 
Reply With Quote
 
Patrick.O.Ige
Guest
Posts: n/a
 
      04-25-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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,);
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in nesting datagrid in to a nested datagrid sajvargs ASP .Net 0 02-20-2005 12:17 AM
problema ultima pagina datagrid asp.net/problem last page datagrid roberto ASP .Net Datagrid Control 1 02-17-2005 07:50 AM
DataGrid edited values problem and autogenerated columns in DataGrid problem John Walsch ASP .Net Web Controls 0 07-09-2004 03:03 PM
Call Datagrid Command column outside datagrid Dave ASP .Net 0 11-20-2003 11:11 AM
To all Gurus: How can I edit/update a DataGrid in a DataGrid (nested DataGrid)? Possible? Andreas Klemt ASP .Net Datagrid Control 0 10-08-2003 01:19 AM



Advertisments