473,386 Members | 1,647 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,386 software developers and data experts.

gridview (refresh gridview and edit row)

145 100+
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.databind() 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 7247
tlhintoq
3,525 Expert 2GB
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 100+
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.DataKeys[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 Expert 2GB
What error are you getting?
Is it the exception you are throwing on line 28?
Mar 6 '09 #4
dorandoran
145 100+
no is not on line 28. the last part of the code is messed up.
Mar 6 '09 #5
Frinavale
9,735 Expert Mod 8TB
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 100+
Error on line 16

'System.EventArgs' does not contain a definition for 'RowIndex'
Mar 6 '09 #7
tlhintoq
3,525 Expert 2GB
@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 Expert Mod 8TB
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.EventArgs 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.WebControls.GridViewRowEventArgs Object....which does contain this property.

To fix the problem use the GridView.SelectedRow or whatever to access your row.
Mar 6 '09 #9
dorandoran
145 100+
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.EditIndex = r;

but GridView2.EditIndex = 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
tlhintoq
3,525 Expert 2GB
Did you mean for this section to only ever execute once, regardless of the value of totalrows?
Expand|Select|Wrap|Line Numbers
  1. for (int r = 0; r < totalrows; r++)
  2. {
  3.   GridView2.EditIndex = id;
  4.   break;
  5. }
  6.  
It is going to begin with r = 0.
It is going to set GredView2.EditIndex to the value of id
Then the for loop breaks
and execution continues to the next line

Even if it did execute many times it would be taking the same value of id, and placing it in the same GridView2.EditIndex over and over and over. There are no changing values inside your loop. Effectively doing nothing different each time through the loop.

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.EditIndex = r;
The first time through the loop r is equal to 0, which is your first array element. So this makes sense.

but GridView2.EditIndex = id; does not do anything.
Maybe id never gets set to any real value that corresponds to how many entries you have. You only set it the one time on line 13. It never changes thereafter.
Mar 6 '09 #11
dorandoran
145 100+
okay the line 13 id is the emp_id number that gets returned from the stored procedure after executing the insert statement. I would like to then refresh the GridView so that the newly created record will appear on the page. Then I would like edit the newly added record. Now forget the row index because it is not going to work. i would like to take the emp_id that I am getting from the sproc and edit that row only. (default sort is by last name, I think I have to sort the grid by emp_id desc then edit the desired record).

Any idea ?????
Mar 7 '09 #12
dorandoran
145 100+
I am still open to ideas. however, I am using this solution for now since I have to move on with the project. Thanks guy. Here is what I did. I truly hate to use sql statement in my code. hard to manage.

string sc = "SELECT [emp_id], [emp_pin], [last_name], [first_name], [hiring_date] FROM [EMP] ORDER BY [emp_id] desc";
SqlDataSource2.SelectCommand = sc;
GridView2.EditIndex = 0;
Mar 7 '09 #13

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

Similar topics

4
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...
3
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...
1
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...
1
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...
0
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"...
5
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...
1
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"...
0
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...
2
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. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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,...

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.