Home All Groups Group Topic Archive Search About

DataGrid Edit/Update problem

Author
22 Apr 2006 8:07 PM
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

Author
23 Apr 2006 12:09 AM
JT
You could add a datagridtablestyle and then explicitly add your
datagridcolumnstyles to this.  One of the properties of the
datagridcolumnstyle is .ReadOnly, which you can set to true.

Here's a great article about it:
http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx

--
John


Show quote
"thebison" wrote:

> 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
>
>
Author
23 Apr 2006 2:37 PM
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
Author
24 Apr 2006 4:41 AM
JT
Hi Al,
Is your datagrid bound to a datasource?  If so, could you not just use the
datasource to retrieve your values, as opposed to looking at cell values?
--
John


Show quote
"thebison" wrote:

> 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
>
>
Author
24 Apr 2006 11:18 AM
thebison
Hi John,

My DataGrid is bound to DataView4 as Data Source, with a RowFilter as
below.

this.sqlDataAdapter4.Fill(this.dsTimeSheetTest2);
dataView4.RowFilter = "ResourceID  = '" +
this.listResName.SelectedValue +
"' AND TimePeriodID = '" + this.listTimePeriod.SelectedValue + "'";
DataGrid1.DataBind();

How would I go about using the datasource to retrieve the value of the
TimeSheetID, as you have suggested?
(Sorry, I'm still a relative newbie to .NET!....)

Many thanks

Al
Author
25 Apr 2006 2:09 AM
JT
If you are using the dataAdapter to perform your updates, you could set the
update command of the adapter to be your sproc NewUpdateTimeSheet.  You could
then map the parameters to their source columns in the dataTable that
underlies your DataView4 (this.dsTimeSheetTest2.Table(0)).  After that, all
you should have to do is call the dataAdapter's update method without
explicitly setting sproc parameter values.  If you still want to explicitly
set values, the value you are seeking is DataView(n)("TimeSheetID"), where n
is an integer representing the row in the datatable you are updating.  In
fact, you don't even need to display your TimeSheetID in the datagrid - you
can have columns of your datatable not display in your grid by just not
creating a datagridcolumnstyle for them in your custom datagridtablestyle.

--
John


Show quote
"thebison" wrote:

> Hi John,
>
> My DataGrid is bound to DataView4 as Data Source, with a RowFilter as
> below.
>
> this.sqlDataAdapter4.Fill(this.dsTimeSheetTest2);
> dataView4.RowFilter = "ResourceID  = '" +
> this.listResName.SelectedValue +
> "' AND TimePeriodID = '" + this.listTimePeriod.SelectedValue + "'";
> DataGrid1.DataBind();
>
> How would I go about using the datasource to retrieve the value of the
> TimeSheetID, as you have suggested?
> (Sorry, I'm still a relative newbie to .NET!....)
>
> Many thanks
>
> Al
>
>

AddThis Social Bookmark Button