Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi goodevening everybody,

I am Raghavendra i have no idea on how to bind gridview data to textboxes when i click a particular record from the gridview.

please help me.
Posted
Updated 14-Feb-12 0:03am
v2
Comments
Varun Sareen 14-Feb-12 6:08am    
Edit For: Removed bold and underline tag
Anuja Pawar Indore 14-Feb-12 9:23am    
Not clear. provide your code

C#
   for (int i = 0; i < gdv.Rows.Count; i++)
            {
TextBox txtBox1 = (TextBox)gdv.Rows[i].FindControl("TxtBoxColName");

         }
 
Share this answer
 
u can do it by row databound and js
here is sample code

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
e.Row.Attributes.Add("onclick", "rowOnClick(" + textBox1.ClientID + "," + textBox2.ClientID + ",this);");
}
}


in js add this code

JavaScript
function rowOnClick(txt1,txt2,th)
    {
txt1.value=th.children[0].innerText;
txt2.value=th.children[1].innerText;
}
 
Share this answer
 
hi here is the code as per ur requirement.
gridview.aspx design page

 <asp:gridview id="GridView1" runat="server" datakeynames="ID" xmlns:asp="#unknown">
            onrowcommand="GridView1_RowCommand"&gt;
            <columns>
                <asp:buttonfield commandname="View" text="View" /;
            </columns>
        </asp:gridview>
<u>gridview.aspx.cs page</u>
#region  Bind GridView();
    private void  BindGridView()
    {
        DataTable dt = new DataTable();

    SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());

    try

    {
       connection.Open();

       string sqlStatement = "SELECT * FROM Registration";

       SqlCommand cmd = new SqlCommand(sqlStatement, connection);

       SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);

       sqlDa.Fill(dt);

       if (dt.Rows.Count; 0)

   {

       GridView1.DataSource = dt;

       GridView1.DataBind();

    }

    }

    catch (SqlException ex)

    {
 
      string msg = "Fetch Error:";

      msg += ex.Message;

    throw new Exception(msg);

    }

    finally

    {

      connection.Close();

    }

    }

#endregion


this code for gridview1_RowCommand:
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "View")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int id = Convert.ToInt32(GridView1.DataKeys[index].Value);
            Response.Redirect("ViewinTextbox.aspx?UserID=" + id);
        }

    }
viewinTextbox.aspx
place here text box field 


then in viewintexbox.aspx.cs give this code in page load.

string sID = Request.QueryString["UserID"].ToString();
        int id = 0;

        int.TryParse(sID, out id);



      SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());
        con.Open();
        SqlCommand cmd=new SqlCommand();
        cmd.Connection=con;
        cmd.CommandType=CommandType.StoredProcedure;
        cmd.CommandText="usp_view";

        SqlParameter paraFirstName = new SqlParameter("@ID", SqlDbType.NVarChar);
        paraFirstName.Value=id;
        cmd.Parameters.Add(paraFirstName);

        SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sqlDA.Fill(dt);

        if (dt.Rows.Count; 0)
        {
            textboxFirstName.Text = dt.Rows[0]["FirstName"].ToString();
            textboxMiddlename.Text = dt.Rows[0]["MiddleName"].ToString();
           

        }



here is stored procedure query
SQL
create PROCEDURE usp_view
@ID int

AS
--set @ID=(SELECT count(*) from tableName where ID=@ID)
SELECT * from [tableName] where ID=@ID





it will work 100% .accept solution.
 
Share this answer
 
v4
Comments
Madhugundi 14-Feb-12 7:15am    
i am very sorry for incomplete question, thank you for your effort for my doutt but you given the solution for cross page submission i need postback submmission i.e in my webpage i have four textboxes, one gridview and one button(name=save) when i click the save button 1)textboxes data should be save into database abd also display into the gridview.
2) when i click any row into the gridview the row data should be bind to textboxes with in the page. please help me
ythisbug 29-Jun-12 0:40am    
ok so wat u have to do is..in page load
If(!IsPostBack)
{
BindGrid();
//this will display records from database to grid
}

then in save click button after your save query
BindGrid();
just try this..hope it will work 100%
This IS not the direct solution of your problem but I think after learning this u will able to solve this by your self.

GridView all in one[^]
 
Share this answer
 

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