473,395 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DataGrid Edit/Update problem

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
3 2242
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
by: Sky | last post by:
Hello: Another question about trying to wring functionality from a DataGrid... Have a DB table of "Contacts" -- 14 or more fields per record Show in datagrid -- but only 5 columns (First,Last,...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
0
by: Steve | last post by:
I have a datagrid that is created at run time DataGrid dgG = new DataGrid(); BoundColumn bcB; dgG.CellPadding = 5; dgG.CellSpacing = 0; dgG.GridLines = GridLines.Both; dgG.CssClass =...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
5
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
5
by: Tina | last post by:
the Edit, Update, Cancel, and Delete buttons in my datagrid are causing validation elsewhere on the page. I want to specify that these buttons should not cause validation but they have no design...
0
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.