phalanx
phalanx

Reputation: 497

Gridview: editing after searching

MY search button is linked to a GridvieW, which has an edit button on every row. When I press search button, data changes and a databind() occurs. After that, If I try to use the edit button it displays another row to edit, not the selected one(biggest problem).Both buttons work well when tested separately, but not one after another. I solved that removing GridView1.DataBind() from edit button event, but then It will require 2 clicks to display the edit template(another problem).

EDIT: I guess teh problem is in the search button. Can you give a good search code that doesn't depend of sqldatasource?

'Where data is loaded into GV
Dim SqlDataSource1 As New SqlDataSource

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    SqlDataSource1.SelectCommand = "SELECT * FROM [TABLE]"
    SqlDataSource1.ConnectionString = "Conn String"

    If Not IsPostBack Then   

        Dim conn As New SqlConnection
    conn.ConnectionString = con.GetConnectionString
    Dim cmd As New SqlCommand()
    cmd.CommandText = "SELECT [AREA], [LEADER_USER] FROM [AREA]"
    cmd.CommandType = CommandType.Text
    cmd.Connection = conn
    conn.Open()
    Dim adpt As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    adpt.Fill(ds)
    GridView1.DataSource = ds
    GridView1.DataBind()
    conn.Close()
    End If

End Sub

'Search button
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    Try
        SqlDataSource1.SelectCommand = "SELECT * FROM TABLE WHERE id LIKE @id"
        SqlDataSource1.SelectParameters.Clear()
        SqlDataSource1.SelectParameters.Add(New Parameter("id", DbType.String, "%" + txtSearch.Text + "%"))
        GridView1.DataSource = SqlDataSource1
        GridView1.DataBind()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

'Edit button
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)

    GridView1.EditIndex = e.NewEditIndex
    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()

End Sub

Markup:

<asp:TextBox ID="TextBox1" runat="server" BackColor="#D9ECFF" 
                                        style="height: 20px; width: 186px" AutoPostBack="True"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" BackColor="#0066cc" 
                                        BorderColor="#0066cc" BorderStyle="Outset" Font-Bold="True" ForeColor="White" 
                                        style=" height: 26px; width: 56px" Text="Search"  />

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
AutoGenerateColumns="False" CellPadding="4" OnRowEditing="EditRow" 
OnRowCancelingEdit="CancelEditRow" DataKeyNames="AREA" DataMember="DefaultView">

  <Columns>
     <asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True" 
                                        SortExpression="AREA" />                                   

      <asp:TemplateField HeaderText="LEADER_USER" SortExpression="LEADER_USER">
                     <ItemTemplate><%#Eval("leader_user")%></ItemTemplate>
                      <EditItemTemplate>
                          <asp:TextBox ID="txtleaderuser" runat="server" Text='<%#Eval("leader_user")%>'/>
                      </EditItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField>                                    
           <ItemTemplate>
                  <asp:ImageButton ID="editButton" runat="server" CommandName="Edit" 
                                                ImageUrl="images/pencil1.png" Text="Edit" ToolTip="Edit" />
            </ItemTemplate>
             <EditItemTemplate>
                   <asp:Button ID="BtnUpdate" runat="server" CommandName="Update" 
                                                Text="Update" />
                   <asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" 
                                                Text="Cancel" />
              </EditItemTemplate>
             </asp:TemplateField>

      </Columns>
    </asp:GridView>

Please help me. How are these 2 gridview functionalities supposed to be coded to work together? This is the only way I know how to do this, but any idea is ok for me if it works. If you are a C# guy, you ca use a C#-to VB converter: http://www.developerfusion.com/tools/convert/csharp-to-vb/

Upvotes: 0

Views: 2652

Answers (2)

ghane nikraftar
ghane nikraftar

Reputation: 81

you can add Where statement in page_load by if command and delete where in btnSearch_Click function. like this

if (txtSearch.Text.length()>0)
  SqlDataSource1.SelectCommand += "WHERE id LIKE @id"

then it works properly

Upvotes: 0

briskovich
briskovich

Reputation: 690

Try this :

  fill_grid()
        {
// populate your grid
           SqlCommand cmd = new SqlCommand();

       cmd.CommandText = " your select statement  ";


       cmd.CommandType = CommandType.Text;
       cmd.Connection = this.sqlConnection1;
       this.yourConnection .Open();
       SqlDataAdapter adpt = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       adpt.Fill(ds);
       yourGrid.DataSource = ds;
       yourGrid.DataBind();

       this.sqlConnection1.Close();
    }

Then hook to the button click event of your search button like this :

 yourButton_Click ((object sender, EventArgs e)
       {
           GridViewRow clickedRow = ((Button)sender).NamingContainer as GridViewRow;

    //Then find your parameter from your textbox in the clicked row 

           TextBox yourbox  = (TextBox)clickedRow.FindControl("your_box");

           //Here is where you would all your search logic whatever that is 

       }

Then you could still use your row_updating event independently of the search button click event.

Upvotes: 1

Related Questions