473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gridview (refresh gridview and edit row)

145 New Member
I followed this link to add new record from gridview. So far it's good
but I need to modify the last piece to edit the record that I will
supply the record id.

http://asptutorials.net/ASP/gridview...dit-blank-row/


I am using stored proc to insert a new record and returning the id.
Now I can get the id in my asp.net (c$) environment. I added a label
and I was able to populated the label with the newly added record's
id.


I would like to use this new id to following code to edit. Also, my
gridview does not refresh after adding the new record
(gridview2.data bind() not doing what it supposed to).


Instead of getting empty row, i would like to get a row by record id
and put it in edit mode.
Expand|Select|Wrap|Line Numbers
  1. int totalrows = GridView1.Rows.Count; 
  2. for (int r = 0; r < totalrows; r++) 
  3.    if (GridVIew1.DataKeys[r].Values[1].ToString() == "") 
  4.    { 
  5.       GridVIew1.EditIndex = r; 
  6.       break; 
  7.    }
  8. }
  9.  
Mar 5 '09 #1
12 7258
tlhintoq
3,525 Recognized Expert Specialist
Ok. Lots of statements in your post. No questions though. What part do you need help with? What error are you getting if any? What variations have you tried so far that has been close, but not quite?
Mar 6 '09 #2
dorandoran
145 New Member
I having 2 issues.

1. My datagrid is not refreshing after I insert a record.
2. I would like to open the record in edit mode that i have just inserted. I am getting the id back. (refering to last paragraph of my post). I change this line to this GridVIew1.DataK eys[r].Values[1].ToString() == id) but not working. getting error.

Here is the code
Expand|Select|Wrap|Line Numbers
  1.     protected void btnAddRecord_Click(object sender, EventArgs e)
  2.     {
  3.         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
  4.         {
  5.             try
  6.             {
  7.                 string sp = "sp_AddUser";
  8.                 SqlCommand command = new SqlCommand(sp, conn);
  9.                 command.CommandType=CommandType.StoredProcedure;
  10.                 conn.Open();
  11.                 int id; 
  12.                 id =  Convert.ToInt32(command.ExecuteScalar());
  13.                 Label1.Text = Convert.ToString(id);
  14.                 GridView2.DataBind();
  15.                 int totalrows = GridView2.Rows.Count;
  16.     int empId = Convert.ToInt32(dgEmployees.DataKeys e.RowIndex].Value);
  17.       for (int r = 0; r < totalrows; r++)
  18.       {
  19.         if (GridVIew1.DataKeys[r].Values[1].ToString() == id)
  20.          {
  21.                GridVIew2.EditIndex = id;
  22.                break;
  23.         }
  24.          }                
  25.            }
  26.             catch (SqlException sqlEx)
  27.             {
  28.                 throw sqlEx;
  29.             }
  30.             finally
  31.             {
  32.                 conn.Close();
  33.                 GridView2.DataBind();
  34.             }
  35.  
  36.  
  37.     }
  38. }
  39.  
Mar 6 '09 #3
tlhintoq
3,525 Recognized Expert Specialist
What error are you getting?
Is it the exception you are throwing on line 28?
Mar 6 '09 #4
dorandoran
145 New Member
no is not on line 28. the last part of the code is messed up.
Mar 6 '09 #5
Frinavale
9,735 Recognized Expert Moderator Expert
What is the error?
What line does the "last part of the code" start messing up on?
How does it mess up?
Mar 6 '09 #6
dorandoran
145 New Member
Error on line 16

'System.EventAr gs' does not contain a definition for 'RowIndex'
Mar 6 '09 #7
tlhintoq
3,525 Recognized Expert Specialist
@dorandoran
That makes sense.
'e' is the EventArgs of the Button.Click event (line 1). From when you clicked on the btnAddRecord. A button does not have a "RowIndex" property.

You probably just grabbed the wrong variable. Who *does* have a "RowIndex"? dgEmployees maybe?

HINT: When you started typing 'e.' intellisense would have given you a list of properties that exist within 'e'. If RowIndex did not appear in that list, then it does not exist in 'e'.
Mar 6 '09 #8
Frinavale
9,735 Recognized Expert Moderator Expert
Oh that's because you're in a method that handles Button Click Event..........

In that case, don't use e.RowIndex. The "e" parameter in this case is a System.EventArg s Object which does not contain information about the GridView's RowIndex. The when you are handling events that are generated by a GridView the "e" parameter is a System.Web.UI.W ebControls.Grid ViewRowEventArg s Object....which does contain this property.

To fix the problem use the GridView.Select edRow or whatever to access your row.
Mar 6 '09 #9
dorandoran
145 New Member
1. When I click on the "Add" button, I can see a record being added but gridview2 does not get refreshed.

2. I am using the code below to edit the newly added record.
when I put an 'r' for this code then the row gets added and the very first record on the grid becomes editable

GridView2.EditI ndex = r;

but GridView2.EditI ndex = id; does not do anything.


Expand|Select|Wrap|Line Numbers
  1.     protected void btnAddRecord_Click(object sender, EventArgs e)
  2.     {
  3.         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
  4.         {
  5.             try
  6.             {
  7.                 string sp = "sp_AddUser";
  8.                 SqlCommand command = new SqlCommand(sp, conn);
  9.                 command.CommandType=CommandType.StoredProcedure;
  10.                 conn.Open(); 
  11.  
  12.                 int id; 
  13.                 id =  Convert.ToInt32(command.ExecuteScalar());
  14.                 Label1.Text = Convert.ToString(id);
  15.  
  16.                 GridView2.SelectedIndex = -1;  // NOT REFRESHING
  17.                 GridView2.DataBind();              // NOT refreshing gridview
  18.                 GridView2.SelectedIndex = -1;  // NOT WORKING
  19.  
  20.                 int totalrows = GridView2.Rows.Count;
  21.                 Label2.Text = Convert.ToInt32(totalrows);
  22.                 for (int r = 0; r < totalrows; r++)
  23.                 {
  24.                         GridView2.EditIndex = id;
  25.                         break;
  26.                 }
  27.              }
  28.             catch (SqlException sqlEx)
  29.             {
  30.                 throw sqlEx;
  31.             }
  32.             finally
  33.             {
  34.                 conn.Close();
  35.                 GridView2.DataBind();
  36.             }
  37.  
  38.  
Mar 6 '09 #10

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

Similar topics

4
2609
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I re-format the updated row fields. I have looked at gridView.rowUpdated method, but cannot figure out how....
3
11751
by: Simon Strandgaard | last post by:
Hi group, my setup: I have a GridView that extracts data from my table (a SqlDataSource). For each row, I have an 'edit' button and a 'delete' button. The 'edit' button opens a popup, through which one can insert and edit the table. The 'delete' button deletes the row.
1
3105
by: coynej60 | last post by:
I have a GridView that is bound to a SqlDataSource that uses Stored Procedures for its Select, Insert, Update and Delete Commands. The Gridview only contains one visible column (PK Identity column is hidden). There is a textbox and button that I use to submit a new row. Everything works fine, however when I submit a new row it does not show...
1
7867
by: Mike P | last post by:
When you use a SqlDataSource to hook up data to a GridView it seems to be to be very inflexible. For example, I want to create my own Delete button with my own code and I also want to create a popup warning for the user before delete takes place. Whenever I do this with a SqlDataSource, I get the error 'Deleting is not supported by data...
0
2468
by: Mike P | last post by:
I am trying to edit a gridview while using paging, but whenever I try to edit a row on a page other than page 1, I get an error. Here is my gridview and my code : <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="UserKey" AllowSorting="True" HeaderStyle-Height="24px" AutoGenerateColumns="false"...
5
25302
by: maurban | last post by:
Hi there experts, I have a gridview with a couple textboxes and a dropdownlist. I'm trying to insert a default value into my database driven dropdownlist. I'm doing this in the rowdatabound event. My problem is that my code only works for the very first row in the gridview. For the first row, when I press "edit", my gridview goes to edit...
1
10385
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID" OnRowCreated="ContactHistoryGrid_RowCreated" CssClass="GridViewTable" GridLines="None" CellSpacing="1" CellPadding="3" AllowSorting="True" AllowPaging="True">...
0
4813
by: mike0870 | last post by:
Hi, I've been at this one for hours and cannot not find any posts of anyone having the same problem. Ther scenario is, I need to fill a drop down box with a value in the grid row to pass to the select statement whcih returns the rows to the drop down. Its the problem is happening in the ObjectDataSource_Selecting Event of the dropdown control...
2
3239
by: DC | last post by:
Hi, I am using a GridView to present data in a DataTable, which I store only in ViewState and when the user hits the "OK" button the rows in the DataTable will be used to execute transactions. I need the ability to insert new rows (which is easily done with DataTable.Rows.Add(newRow) and rebinding) and then present a row with different...
0
7410
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...
1
7437
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...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
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...
1
1901
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
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.