how to use rowcommand to select the value from gridview and bind to textbox

Raj_Chennai
Posted by Raj_Chennai under ASP.NET category on | Points: 40 | Views : 7844
Default.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand" 
>
<Columns>
<asp:BoundField DataField="name" HeaderText="name" />
<asp:BoundField DataField="dob" HeaderText="dob" />
<asp:BoundField DataField="gender" HeaderText="gender" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

default.aspx.cs
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["raj"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select name,dob,gender from personaldetails", con);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder scb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "cmpdetails");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
TextBox1.Text = Server.HtmlDecode(row.Cells[2].Text);
}
}

Comments or Responses

Login to post response