Click here to Skip to main content
12,406,342 members (31,602 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:
<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:
<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:
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 9-Nov-12 21:02pm
Updated 9-Nov-12 21:25pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 + ".";
 
   }
 
 }
  Permalink  
Comments
Member 9479793 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
Member 9479793 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160730.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100