473,395 Members | 1,675 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.

GridView Editing

Ok, so since my last post I've tested the Gridview and DataView controls and it appears as though those would work a little better. However, I'm having difficultly implementing the "Edit mode" on those controls to allow the user to edit the data. I can't really seem to find a "step by step" example of of how to do it. Any assistance would be great.

Thank again
Mar 21 '10 #1
10 2954
Frinavale
9,735 Expert Mod 8TB
What exactly are you having problems with?
If you want to edit a row then implement a method that handles the GridView.RowEditing event and set the GridView.EditIndex to the e.NewEditIndex to edit the row that was selected for editing.

For example:

Expand|Select|Wrap|Line Numbers
  1.         protected void TableGridView_RowEditing(Object sender, GridViewEditEventArgs e)
  2.         {
  3.             TableGridView.EditIndex = e.NewEditIndex;
  4.         }
  5.         protected void TableGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
  6.         {
  7.             TableGridView.EditIndex = -1;
  8.         }
Please post new questions in their own thread.
I have split this question off of your other thread on TextBox Loop C#

-Frinny
Mar 25 '10 #2
I need to write the matrix to the screen and let the user update whichever cell they need (i.e. textboxes). Once they are complete they would click a submit button to update the database. An example is below.

Mar 26 '10 #3
Frinavale
9,735 Expert Mod 8TB
Hmm so you need every cell to be editable.

In this case I think that you are going to want to use a GridView for editing purposes (where every element is displayed in TextBoxes) and a GridView for display purposes...simply because I'm not sure how you would make every row editable at the same time in edit mode.

Do you see what I'm getting at?

-Frinny
Mar 31 '10 #4
Ok, so I would add the textboxes to the gridview like so right?

Expand|Select|Wrap|Line Numbers
  1.     <asp:Gridview id="ItemsGrid" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateEditButton="False" AutoGenerateColumns="False">
  2.  
  3.      <Columns>  
  4. <asp:TemplateField HeaderText="R1C1" SortExpression="r1c1">  
  5. <EditItemTemplate>  
  6. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("r1c1") %>'>
  7. </asp:TextBox>  
  8. </EditItemTemplate>  
  9.  
  10. <ItemTemplate>  
  11. <asp:Label ID="Label1" runat="server" Text='<%# Bind("r1c1") %>'>
  12. </asp:Label>  
  13. </ItemTemplate> 
  14. </asp:TemplateField> 
  15.      </Columns> 
  16.  
  17.     </asp:Gridview>
  18.  
Because all I get is a readonly display of the data. I don't see the textboxes anywhere. Thanks again for all the help.
Apr 1 '10 #5
Frinavale
9,735 Expert Mod 8TB
That's because you put the TextBox in the <EditItemTemplate> section of your TemplateField.

Move the TextBox to the <ItemTemplate> section instead (you don't have one right now).

:)

-Frinny
Apr 1 '10 #6
Great, I got it working with the following...

Expand|Select|Wrap|Line Numbers
  1.      <Columns>  
  2. <asp:TemplateField>   
  3.  
  4. <ItemTemplate>
  5. <asp:TextBox ID="TextBox1" runat="server" size="2" Text='<%# Bind("r1c1") %>'>
  6. </asp:TextBox> 
  7. <asp:TextBox ID="TextBox2" runat="server" size="2" Text='<%# Bind("r1c2") %>'>
  8. </asp:TextBox> 
  9. <asp:TextBox ID="TextBox3" runat="server" size="2" Text='<%# Bind("r1c3") %>'>
  10. </asp:TextBox> 
  11. <asp:TextBox ID="TextBox4" runat="server" size="2" Text='<%# Bind("r1c4") %>'>
  12. </asp:TextBox>     
  13. </ItemTemplate>
  14.  
  15. </asp:TemplateField> 
  16.      </Columns> 
  17.  
However, I have 102 fields in the SQL table. Do I have to insert 102 textbox fields? Is there any easier way? Is there a If or while I can execute?
Apr 6 '10 #7
Frinavale
9,735 Expert Mod 8TB
Oh...102 is quite a bit.

You could consider doing this dynamically I guess but it's not that straight forward. I had to do something similar before...I had 4*255, or 8*255, or 16*255 items that I needed to display as links. I decided to create a TemplateItem with links in it dynamically but it was not as straightforward as I thought it was going to be....

Mind you with TextBoxes you don't have to worry about events being thrown by the TextBox so it might be a bit easier.


Doing this by hand will be a lot easier to implement than doing it dynamically...but you can do this dynamically if you choose to.

-Frinny
Apr 6 '10 #8
Great, can you give me a starting point for creating the textboxes dynamically? How would I start that?

Thanks
Apr 6 '10 #9
Frinavale
9,735 Expert Mod 8TB
Well, like I said this is not exactly the easiest thing to do (the first time anyways) because there are a lot of things that you have to keep in mind when you implement this.

The first thing I suggest you do is read over this quick overview on how to use dynamic controls in ASP.NET.

The important thing to take from this is that if you want to be able to retrieve Text from your dynamic TextBoxes...or if you want to be able to handle an Event that some control raised...you need to instantiate the dynamic controls in the Page_Init event.

In your case you are going to be dynamically adding TemplateFields ("columns") to your GridView in the Page_Init event....so you need to know how many columns you need to add.

The Page_Init event occurs very early in the life cycle. You don't have a lot of things available to you at that time so be aware that things like "IsPostback" won't work properly. This is because, things like "IsPostback" are initialized after the ViewState for the page is loaded, and the Page_Init event occurs just before the ViewState is loaded. You can still use things like Session or access cookies or use the query string at this point though. The whole reason why it's important that your instantiate your dynamic controls in the Page_Init is so that the objects exist when the ViewState is loaded...in other words if your TextBoxes don't exist when the ViewState is loaded for them, then your TextBoxes will not contain any Text (which may be loaded from the ViewState).

Ok, now that that very important point has been covered, I recommend that you look over the ITemplate Interface. This interface lets you define a class that will you can use as the TemplateField. You will have to create a custom "TemplateField" class and in there add your TextBoxes to the class.

You should probably also look at the INamingContainer Interface so that you get a good idea of what's going on in general (you should probably read this first actually).

Both of these topics might take you a while to go through.
Like I said this is not exactly easy to do the first time round because there is a lot of new topics and concepts to grasp before you will get a working solution.

-Frinny
Apr 7 '10 #10
I'm grateful for all the help. It's going to take me a while to go through all this.

Thanks Again
Apr 22 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: j.zascinski | last post by:
Hi, i have a "simple" problem with gridview, please help me :) i want to have gridview which is binded to a datatable (or a dataset). i can show the data from the dataset in the gridview and i can...
4
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2...
1
by: Kyle K. | last post by:
I would like to build my data pages such that the top of the page contains a GridView showing the data with 'Enable Select = true'. Below that I would like to have a FormView, that by default is...
4
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
3
by: cpnet | last post by:
I have a GridView which I'm populating from an ObjectDataSource (give the GridView a DataTable). The GridView will have about 20 rows, and only one editable column. The editable column consists...
1
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
Hi, I have a gridview which I added a <asp:CommandField EditText="E" CancelText="C" UpdateText="U" ButtonType="Link" ShowEditButton="True" /> my gridview looks like this <asp:GridView...
1
by: pieandpeas | last post by:
he selectedindex of my gridview is always one behind for example, if i click on the EDIT.gif on row 1, then 5 , then 7, if i break the code and check the selectedindex, it is equal to 5. if i...
5
by: =?Utf-8?B?Z3V5?= | last post by:
How do you enable editing in a GridView programatically rather than via its Tasks menu? Guy
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.