如何将girdview数据绑定到文本框 [英] How to bind girdview data to textboxes

查看:76
本文介绍了如何将girdview数据绑定到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚安,

我是Raghavendra,当我从gridview单击特定记录时,我不知道如何将gridview数据绑定到文本框.

请帮助我.

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.

推荐答案

这不是您的问题的直接解决方案,但我认为在学习了这一点之后,您将能够自行解决.

GridView全部集成 [
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[^]


您好,这里的代码是您的要求.
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

create PROCEDURE usp_view
@ID int

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






它将可以100%接受解决方案.






it will work 100% .accept solution.


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

         }


这篇关于如何将girdview数据绑定到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆