title: asp .net gridview search and edit
body: <p>I have a problem with editing row after searching. When I find my user and click button Edit in gridview it turns me first record but not in edit mode. After cliking second time on my search button I have my record in edit mode. I've tried to solve it by using GridViewRow.RowState Property regarding to this link: <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowstate%28v=vs.110%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowstate%28v=vs.110%29.aspx</a>. When I was clicking Edit it does nothig but after second click I have error "Failed to load viewstate"
  I have tried to solve it by adding on page EnableViewState="true", but all the time I had the same error. My BindData() method looks like this: </p>
  
  <pre><code>private void BindData()
          {
              using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString))
              {
                  string display = "SELECT * FROM User";
                  using (SqlCommand cmd = new SqlCommand())
                  {
                      con.Open();
                      cmd.Connection = con;
                      cmd.CommandType = CommandType.Text;
                      cmd.CommandText = display;
                      SqlDataAdapter da = new SqlDataAdapter(cmd);
                      DataSet ds = new DataSet();
                      da.Fill(ds);
                      GridView1.DataSource = ds;
                      GridView1.DataBind();
                  }
              }
  </code></pre>
  
  <p>and search method looks like this:</p>
  
  <pre><code> protected void DisplayUser(string strSearch)
      {
          SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
  
          SqlCommand cmd = new SqlCommand("SearchUsers", con);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@SearchUser", strSearch);
          cmd.Connection.Open();
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }
  </code></pre>
  
  <p>I also tried to change method DisplayUser instead of void to DataSet but still I had the same error. 
  My StoredProcedure looks like this:</p>
  
  <pre><code>ALTER PROCEDURE dbo.SearchUsers
  </code></pre>
  
  <p>@SearchUser varchar(50)
  AS
      SELECT *
      FROM [User]
      WHERE ([User].name LIKE '%' +@SearchUser + '%' OR [User].surname LIKE '%' 
      + @SearchUser + '%' OR [User].role LIKE '%' + @SearchUser + '%' OR [User].block 
      LIKE '%' +@SearchUser + '%')</p>
  
  <p>My grid loos like this:</p>
  
  <pre><code>  &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="id_user" onRowCancelingEdit="GridView1_RowCancelingEdit" onRowEditing="GridView1_RowEditing" onRowDataBound="GridView1OnRowDataBound" OnRowUpdating="GridView1_RowUpdating" onRowDeleting="GridView1_RowDeleting" onselectedindexchanged="GV_SelectedIndexChanged" &gt;
      &lt;Columns&gt;
      &lt;asp:TemplateField HeaderText="LP" Visible="false" SortExpression="id_user"  HeaderStyle-Width="5px"&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:Label ID="id_uzytkownika" runat="server" Text='&lt;%# Eval("id_user")%&gt;'&gt;&lt;/asp:Label&gt;
       &lt;/ItemTemplate&gt;
     &lt;/asp:TemplateField&gt;
    &lt;asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" ReadOnly="true" /&gt;
    &lt;asp:BoundField DataField="surname" HeaderText="Surname" SortExpression="surname" ReadOnly="true" /&gt;
     &lt;asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" ReadOnly="true" /&gt;
     &lt;asp:BoundField DataField="login" HeaderText="Login" SortExpression="login" ReadOnly="true" /&gt;
        &lt;asp:TemplateField HeaderText="Password" HeaderStyle-Width="80px" SortExpression="password"&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:Label ID="LblPass" runat="server" Text='&lt;%#Eval("password")%&gt;'&gt;&lt;/asp:Label&gt;
      &lt;/ItemTemplate&gt;
      &lt;EditItemTemplate&gt;
          &lt;asp:TextBox ID="TxtPass" runat="server" Text='&lt;%#Eval("password")%&gt;'&gt;&lt;/asp:TextBox&gt;
      &lt;/EditItemTemplate&gt;
     &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Role" &gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblRole" runat="server" Text='&lt;%#Eval("role") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="DrpDwnRole" runat="server"&gt;
                      &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Block" HeaderStyle-Width="10%"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblBlock" runat="server" Text='&lt;%#Eval("block") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:CheckBox ID="ChckBxBlock" Text="Block" runat="server"&gt;&lt;/asp:CheckBox&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" /&gt;
                      &lt;asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" /&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
               &lt;asp:TemplateField HeaderText="Delete" HeaderStyle-Width="5%"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:LinkButton ID="btnDelete" Text="Usuń" runat="server" CommandName="Delete" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Can some one give me some advices how to sole it?</p>
  
  <p>RowEditing - it is now, but when I wanted to use row stte it looks like on the link at teh beginning of the question.</p>
  
  <pre><code>   protected void GridViewKierUzytkownik_RowEditing(object sender, GridViewEditEventArgs e)
      {
       GridView1.EditIndex = e.NewEditIndex;
      BindData();
  
      }
  </code></pre>
  
  <ol>
  <li>I am using VS2010 nad MVC 4 C#.</li>
  <li>You are talkin about edit, cancel and save buttons? I am using them to edit data, update and cancel updating. Basics operations for gridview.</li>
  </ol>