gridview (refresh gridview and edit row)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dorandoran
    New Member
    • Feb 2007
    • 145

    gridview (refresh gridview and edit row)

    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.




    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.
    Code:
    int totalrows = GridView1.Rows.Count; 
    for (int r = 0; r < totalrows; r++) 
    { 
       if (GridVIew1.DataKeys[r].Values[1].ToString() == "") 
       { 
          GridVIew1.EditIndex = r; 
          break; 
       }
    }
    Last edited by Frinavale; Mar 6 '09, 09:25 PM. Reason: Moved to ASP.NET Answers from .NET, Tlhintoq added code tags. [CODE] ... your code goes here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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?

    Comment

    • dorandoran
      New Member
      • Feb 2007
      • 145

      #3
      Please use
      Code:
       ...
      tags

      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
      Code:
          protected void btnAddRecord_Click(object sender, EventArgs e)
          {
              SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
              {
                  try
                  {
                      string sp = "sp_AddUser";
                      SqlCommand command = new SqlCommand(sp, conn);
                      command.CommandType=CommandType.StoredProcedure;
                      conn.Open();
                      int id; 
                      id =  Convert.ToInt32(command.ExecuteScalar());
                      Label1.Text = Convert.ToString(id);
                      GridView2.DataBind();
                      int totalrows = GridView2.Rows.Count;
          int empId = Convert.ToInt32(dgEmployees.DataKeys e.RowIndex].Value);
            for (int r = 0; r < totalrows; r++)
            {
              if (GridVIew1.DataKeys[r].Values[1].ToString() == id)
               {
                     GridVIew2.EditIndex = id;
                     break;
              }
               }                
                 }
                  catch (SqlException sqlEx)
                  {
                      throw sqlEx;
                  }
                  finally
                  {
                      conn.Close();
                      GridView2.DataBind();
                  }
      
      
          }
      }
      Last edited by tlhintoq; Mar 6 '09, 05:48 PM. Reason: [CODE] ...your code here [/CODE] tags added

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        What error are you getting?
        Is it the exception you are throwing on line 28?

        Comment

        • dorandoran
          New Member
          • Feb 2007
          • 145

          #5
          no is not on line 28. the last part of the code is messed up.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            What is the error?
            What line does the "last part of the code" start messing up on?
            How does it mess up?

            Comment

            • dorandoran
              New Member
              • Feb 2007
              • 145

              #7
              Error on line 16

              'System.EventAr gs' does not contain a definition for 'RowIndex'

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by dorandoran
                Error on line 16

                'System.EventAr gs' does not contain a definition for 'RowIndex'
                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'.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  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.

                  Comment

                  • dorandoran
                    New Member
                    • Feb 2007
                    • 145

                    #10
                    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.


                    Code:
                        protected void btnAddRecord_Click(object sender, EventArgs e)
                        {
                            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
                            {
                                try
                                {
                                    string sp = "sp_AddUser";
                                    SqlCommand command = new SqlCommand(sp, conn);
                                    command.CommandType=CommandType.StoredProcedure;
                                    conn.Open(); 
                    
                                    int id; 
                                    id =  Convert.ToInt32(command.ExecuteScalar());
                                    Label1.Text = Convert.ToString(id);
                    
                                    GridView2.SelectedIndex = -1;  // NOT REFRESHING
                                    GridView2.DataBind();              // NOT refreshing gridview
                                    GridView2.SelectedIndex = -1;  // NOT WORKING
                                
                                    int totalrows = GridView2.Rows.Count;
                                    Label2.Text = Convert.ToInt32(totalrows);
                                    for (int r = 0; r < totalrows; r++)
                                    {
                                            GridView2.EditIndex = id;
                                            break;
                                    }
                                 }
                                catch (SqlException sqlEx)
                                {
                                    throw sqlEx;
                                }
                                finally
                                {
                                    conn.Close();
                                    GridView2.DataBind();
                                }

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Did you mean for this section to only ever execute once, regardless of the value of totalrows?
                      Code:
                      for (int r = 0; r < totalrows; r++)
                      {
                        GridView2.EditIndex = id;
                        break;
                      }
                      It is going to begin with r = 0.
                      It is going to set GredView2.EditI ndex 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.EditI ndex 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.EditI ndex = r;
                      The first time through the loop r is equal to 0, which is your first array element. So this makes sense.

                      but GridView2.EditI ndex = 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.

                      Comment

                      • dorandoran
                        New Member
                        • Feb 2007
                        • 145

                        #12
                        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 ?????

                        Comment

                        • dorandoran
                          New Member
                          • Feb 2007
                          • 145

                          #13
                          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.EditI ndex = 0;

                          Comment

                          Working...