Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to search string(dropdownlist selection) in gridview and search row should be selected.

dropdownlist..
ASP.NET
<asp:TextBox ID="SearchTextBox" runat="server"
                          ></asp:TextBox>

gridview..
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                  onrowdatabound="GridView1_RowDataBound"
                 onselectedindexchanged="GridView1_SelectedIndexChanged1" CellPadding="4"
                 ForeColor="#333333" GridLines="None" AllowPaging="True"
                         onpageindexchanging="GridView1_PageIndexChanging" PageSize="2"
                  >
                  <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                  <Columns>

                  <asp:BoundField DataField="DesigID" HeaderText="Designation ID">
                          <ItemStyle HorizontalAlign="Center" />

                      </asp:BoundField>
                      <asp:BoundField DataField="DesigName" HeaderText="Designation Name">
                          <ItemStyle HorizontalAlign="Left" />
                      </asp:BoundField>

                  </Columns>
                  <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                  <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
           <selectedrowstyle backcolor="LightCyan" forecolor="DarkBlue" font-bold="true"/>
                  <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                  <AlternatingRowStyle BackColor="White" />
              </asp:GridView>

search button.
ASP.NET
<asp:Button ID="SearchButton" runat="server" Text="Search"
                               onclick="SearchButton_Click" />
ASP.NET
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

When I click the button, DesigName should be display on textbox according to desigID in searchtextbox.
Posted
Updated 15-May-12 0:46am
v3

HI ..

Please try like this.

C#
public void bindempage()


    {

        SqlConnection con = new SqlConnection(cons);

        string query = "select age from emp order by age";

        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);

        SqlDataReader dr;
        dr = cmd.ExecuteReader();

        dropdown.DataSource = dr;
        dropdown.DataValueField = "age";
        dropdown.DataBind();
        if (!dropdown.Items[0].Equals("Select Id"))
            dropdown.Items.Insert(0, "Select Id");
 
    
    }



It may be help you..
 
Share this answer
 
v2
Comments
Member 7909353 15-May-12 3:24am    
I have to select the dropdownlist and that string should be search in gridview and display in textbox.
C#
protected void SearchButton_Click(object sender, EventArgs e)
       {
           string strText = SearchTextBox.Text.Trim();
           if (strText == "")
           {
               flag = false;
               GridView1.PageIndex = 0;
               ViewButton_Click(sender, e);


           }
           else
           {
               GridViewRow row;

               //for (int i = 0; i < GridView1.Rows.Count; i++)
               for (int j = 0; j < GridView1.PageCount; j++)
               {

                   GridView1.PageIndex = j;
                   ViewButton_Click(sender, e);
                   for (int i = 0; i < GridView1.Rows.Count; i++)
                   {
                       row = GridView1.Rows[i];
                       string fl = ((row.Cells[1]).Text);
                       if (strText == ((row.Cells[1]).Text))
                       {
                           flag = false;
                           row.ForeColor = ColorTranslator.FromHtml("red");
                           break;

                       }
                   }
                   if (flag == false)
                   {
                       break;
                   }
               }
               if(flag==true)
                   {
                       GridView1.PageIndex = 0;
                       ViewButton_Click(sender, e);
                   }


           }

       }
 
Share this answer
 
C#
protected void SearchButton_Click(object sender, EventArgs e)
      {
          GridViewRow row;
          string _strText = SearchTextBox.Text.Trim();
          for (int i = 0; i < GridView1.Rows.Count; i++)
          {
              row = GridView1.Rows[i];
              string fl = ((row.Cells[1]).Text);
              if (_strText == ((row.Cells[1]).Text))
              {
                row.ForeColor=ColorTranslator.FromHtml("red");


              }
          }
      }


But it is not searching all pages.Only front page is searched,I want all pages should be search.
 
Share this answer
 
Comments
BobJanova 15-May-12 6:41am    
This should work. Have you tried paging through the results and seeing if all the relevant rows are highlighted?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900