Click here to Skip to main content
15,921,179 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to display grid view row to text box
Posted
Comments
[no name] 27-Jul-11 12:15pm    
Content

Please see my solution which exactly does that Link[^]
 
Share this answer
 
First Page :

Design Page :

XML
<div>
        <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Id") %>'
                            CommandName="view">View</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>


Code Behind :


C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        using (SqlConnection con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=Sample;Integrated Security=True"))
        {
            SqlCommand cmd = new SqlCommand("select * from tblUser", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName == "view")
        {
            int Id = Convert.ToInt32(e.CommandArgument);
            Session["Id"] = Id;
            Response.Redirect("New.aspx");
        }
    }



Add another page with name as New.aspx and follow the below code...

New.aspx

Design Page :


XML
<div>
       <table class="style1">
           <tr>
               <td>
                   Id
               </td>
               <td style="margin-left: 40px">
                   <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
               </td>
           </tr>
           <tr>
               <td>
                   First Name
               </td>
               <td>
                   <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
               </td>
           </tr>
           <tr>
               <td>
                   Last Name
               </td>
               <td>
                   <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
               </td>
           </tr>
       </table>
   </div>



New.aspx.cs

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int Id = Convert.ToInt32(Session["Id"]);
            using (SqlConnection con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=Sample;Integrated Security=True"))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from tblUser where Id=" + Id + "", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtID.Text = Convert.ToString(dr["Id"]);
                    txtFirstName.Text = Convert.ToString(dr["FirstName"]);
                    txtLastName.Text = Convert.ToString(dr["LastName"]);
                }
                con.Close();
            }
        }
    }



SQL Query :

SQL
CREATE TABLE [dbo].[tblUser](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
 CONSTRAINT [PK_tblUser] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
Share this answer
 
v2

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