473,769 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.Data GridCommandEven tArgs e)
{
System.Web.UI.W ebControls.Text Box cId = new
System.Web.UI.W ebControls.Text Box();
System.Web.UI.W ebControls.Text Box cName = new
System.Web.UI.W ebControls.Text Box();
cId = (System.Web.UI. WebControls.Tex tBox) e.Item.Cells[0].Controls[0];
cName = (System.Web.UI. WebControls.Tex tBox)
e.Item.Cells[1].Controls[0];
SqlCommand myCommand = new SqlCommand("New UpdateDept",sql Connection1);
myCommand.Comma ndType = CommandType.Sto redProcedure;
myCommand.Param eters.Add(new SqlParameter("@ DeptID",SqlDbTy pe.Int,4));
myCommand.Param eters["@DeptID"].Value = cId.Text;
myCommand.Param eters.Add(new
SqlParameter("@ DeptName",SqlDb Type.VarChar,50 ));
myCommand.Param eters["@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 2264
You can do something like this in the EditCommand event:-
private void somedatagrid_Ed itCommand(objec t source,
DataGridCommand EventArgs e)
{
Button b = new Button();
b= (Button)e.Item. Cells[4].FindControl("W hat is the name of the
control");
b.Enabled = false;
return;
}

Patrick

"thebison" <al************ @btinternet.com > wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.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.W ebControls.Data GridCommandEven tArgs e)
{
System.Web.UI.W ebControls.Text Box cId = new
System.Web.UI.W ebControls.Text Box();
System.Web.UI.W ebControls.Text Box cName = new
System.Web.UI.W ebControls.Text Box();
cId = (System.Web.UI. WebControls.Tex tBox) e.Item.Cells[0].Controls[0];
cName = (System.Web.UI. WebControls.Tex tBox)
e.Item.Cells[1].Controls[0];
SqlCommand myCommand = new SqlCommand("New UpdateDept",sql Connection1);
myCommand.Comma ndType = CommandType.Sto redProcedure;
myCommand.Param eters.Add(new SqlParameter("@ DeptID",SqlDbTy pe.Int,4));
myCommand.Param eters["@DeptID"].Value = cId.Text;
myCommand.Param eters.Add(new
SqlParameter("@ DeptName",SqlDb Type.VarChar,50 ));
myCommand.Param eters["@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.W ebControls.Data GridCommandEven tArgs e)
{
System.Web.UI.W ebControls.Text Box tId = new
System.Web.UI.W ebControls.Text Box();
System.Web.UI.W ebControls.Text Box tHrs = new
System.Web.UI.W ebControls.Text Box();
//This is the problem line!
tId = (System.Web.UI. WebControls.Tex tBox) e.Item.Cells[4].Controls[0];
tHrs = (System.Web.UI. WebControls.Tex tBox) e.Item.Cells[3].Controls[0];

SqlCommand myCommand = new
SqlCommand("New UpdateTimeSheet ",sqlConnection 1);
myCommand.Comma ndType = CommandType.Sto redProcedure;
myCommand.Param eters.Add(new
SqlParameter("@ TimeSheetID",Sq lDbType.Int,4)) ;
myCommand.Param eters["@TimeSheet ID"].Value = tId.Text;
myCommand.Param eters.Add(new SqlParameter("@ Hrs",SqlDbType. Float,8));
myCommand.Param eters["@Hrs"].Value = Convert.ToSingl e (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.Param eters["@TimeSheet ID"].Value =
e.Item.Cells[4].Controls[0];

however this returned the "System.Argumen tOutOfRangeExce ption:
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
"TimeSheetI D"
<asp:Label ID="TimeSheetID " text='<%# DataBinder.Eval (Container.Data Item,
"TimeSheetI D") %>' Runat="server" BorderWidth="0" >
in your codebehind grab the label id as follows using TimeSheetID.tex t
and assign it to your
myCommand.Param eters["@TimeSheet ID"].Value = TimeSheetID.Tex t
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.goo glegroups.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.W ebControls.Data GridCommandEven tArgs e)
{
System.Web.UI.W ebControls.Text Box tId = new
System.Web.UI.W ebControls.Text Box();
System.Web.UI.W ebControls.Text Box tHrs = new
System.Web.UI.W ebControls.Text Box();
//This is the problem line!
tId = (System.Web.UI. WebControls.Tex tBox) e.Item.Cells[4].Controls[0];
tHrs = (System.Web.UI. WebControls.Tex tBox) e.Item.Cells[3].Controls[0];

SqlCommand myCommand = new
SqlCommand("New UpdateTimeSheet ",sqlConnection 1);
myCommand.Comma ndType = CommandType.Sto redProcedure;
myCommand.Param eters.Add(new
SqlParameter("@ TimeSheetID",Sq lDbType.Int,4)) ;
myCommand.Param eters["@TimeSheet ID"].Value = tId.Text;
myCommand.Param eters.Add(new SqlParameter("@ Hrs",SqlDbType. Float,8));
myCommand.Param eters["@Hrs"].Value = Convert.ToSingl e (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.Param eters["@TimeSheet ID"].Value =
e.Item.Cells[4].Controls[0];

however this returned the "System.Argumen tOutOfRangeExce ption:
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
4885
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 the best method? Do you have a sample of how to do this?
2
2339
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, Fax, Phone, Category). Put an Edit column at the end... Now what?! If you go into Edit mode -- you can only edit 5 cells -- not all the rest of the Record's fields...not enough!
1
4339
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 (watch the layout, some have child controls):
0
473
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 = "SectionTableLines"; dgG.DataKeyField = "PlanWorkOrderID";
4
3496
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 snippet from my .css file: *************************** body { margin:0; padding:0;
5
3247
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 I use an argument to difference between an 'edit' vs. and 'add. But since I have field validation on both 'footer' and 'edit' columns I can't submit my edits since the footer validation kicks in.If I take the validation off then the both work fine...
4
2976
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 linkbuttons which must not cause validation. I have found a setting "causeValidation" to disable the validation. Also on the page, I have a datagrid that I will edit lines on. I can click
5
2056
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 time property of causevalidation. How can I keep them from causing validation? Thanks, T
0
1756
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 for editing and deleting data. When a user clicks on the "Edit" button in the datagrid, Edit() method is called and the appropriate row is changed into textboxes. And if user clicks on the "Update" button, the Update() method is fired. But if...
9
2729
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 code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.