Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a Panel, dropdownlist, textbox and gridview in my design.

I have search code in textbox. While I enter any text in that textbox, I will get that

result in gridview. After displaying the results in that gridview, When I click on any

of that particular row, I need to get that data in a table.

(Table consists of 3 textboxes).

Here is the code. .
Table:
XML
<table>
    <tr>
    <td>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
        <td>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </td>
        <td>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
    </tr>
    </table>

Panel, Dropdownlist and gridview:
XML
<asp:Panel ID="Panel2" runat="server">
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            CssClass="style1" Height="38px" Width="328px">
        <asp:ListItem>Code</asp:ListItem>
        <asp:ListItem>Description</asp:ListItem>
        </asp:DropDownList><br />
        <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"
            AutoPostBack="True" Height="23px" Width="330px"></asp:TextBox>
        <asp:GridView ID="GridView1" runat="server"
            OnRowDataBound="GridView1_RowDataBound"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>
        <asp:CommandField ShowSelectButton="true" ItemStyle-CssClass="visible"/>
        </Columns>
        </asp:GridView>

    </asp:Panel>


In cs file:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        cn.Open();
        string s = "select * from Item_tbl";
        SqlCommand cmd = new SqlCommand(s, cn);                                                                                        
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

        cmd.ExecuteNonQuery();
        cn.Close();
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
         try
        {

            cn.Open();
            if (DropDownList1.SelectedItem.Text == "Code")
            {
                string s = "select * from Item_tbl where Code like '" + TextBox1.Text.ToString() + "' +'%' ";
                SqlCommand cmd = new SqlCommand(s, cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            
                cmd.ExecuteNonQuery();
                cn.Close();
               
                
            }
            else if (DropDownList1.SelectedItem.Text == "Description")
            {
                string s = "select * from Item_tbl where Description like '" + TextBox1.Text.ToString() + "' +'%' ";
                SqlCommand cmd = new SqlCommand(s, cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                cmd.ExecuteNonQuery();
                cn.Close();
            }
                
           
        }
        catch
        {
        }
        finally
        {
            cn.Close();
        }

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor = 'hand';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration = 'none';";
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
//on selecting the results, I have written this below code, but when I have many display results, I am getting only one row i.e., starting row but not a selected row
        if (TextBox1.Text == "")
        {

            TextBox1.Text = GridView1.SelectedRow.Cells[2].Text;
            TextBox2.Text = GridView1.SelectedRow.Cells[3].Text;
            TextBox3.Text = GridView1.SelectedRow.Cells[4].Text;

        }
    }
Posted
Updated 9-Nov-12 21:25pm
v2

1 solution

SQL
void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
 {

  
   GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];

  // You c an cancel the select operation by using the Cancel// property. For this example, if the user selects a customer with // the ID "ANATR", the select operation is canceled and an error message// is displayed.if (row.Cells[1].Text == "ANATR")
   {

     e.Cancel = true;
     MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";

   }

 }
 
Share this answer
 
Comments
Bhargav.456 10-Nov-12 3:28am    
Thanks for the code, But I need to get the selected row data from the gridview1 to the table. How do I do it?
vivektiwari97701 10-Nov-12 3:33am    
write ur code under SelectedIndexChanging event and u can access ur selected row using e argument like e.row or e.newSelectedRow and to get value in TextBox Write

TextBox1.Text = e.Row.Cells[2].Text;like this
Bhargav.456 10-Nov-12 4:30am    
what code should i write under selectindexchanging?
where should i write the code in selectindexchanged or selectindexchanging?
i am not getting clearly?

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