Monday 15 April 2013

How to get the Values of Selected Row from a Gridview in TextBox using ASP.NET

How to get the Values of Selected Row from a Gridview in TextBox using ASP.NET


n this Example you can learn how to get the values of selected row from a Gridview and display the values in textboxes using C# code.
Design:
Add Gridview in .aspx page and bind a Data using SqlDataSource or msaccess.and Add Four Textboxes to display a data in Textboxes when the GridviewRow is Selected.

Set the Gridview AutoGenerateColumns property to 'False'.

You will need to set the Bound column in order to see the text in the cells of the grid view control.
.ASPX Code:

<%@ Page Language="C#" AutoEventWireup="true"  Codevirtual="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 112%;
        }
        .style2
        {
            width: 169px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="background-color: #FF9900; width: 356px;">
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="user_id" Height="264px" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" Width="379px" 
            BackColor="#66CCFF" BorderColor="#FF0066">
            <Columns>
                <asp:BoundField DataField="user_id" HeaderText="id" />
                <asp:BoundField DataField="name" HeaderText="name" />
                <asp:BoundField DataField="fathername" HeaderText="fathername" />
                <asp:BoundField DataField="salery" HeaderText="salery" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
            <SelectedRowStyle BackColor="#FF66FF" />
        </asp:GridView>
    
        &nbsp;&nbsp;&nbsp;&nbsp;
        <table class="style1" style="background-color: #CCFF99; height: 268px;">
            <tr>
                <td class="style2">
                    ID</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Fathername</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Salery</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Select Name</td>
                <td>
    
        <asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="127px">
        </asp:DropDownList>
    
                </td>
            </tr>
        </table>

        <br />

        <br />
    
    </div>
    </form>
</body>
</html>


.CS Code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page 
{

    public void binddata()
    {
        SqlConnection con = new SqlConnection(@"data source=R4R-01\SQLEXPRESS;
 initial catalog= banneth; integrated security=true;");
        con.Open();
        SqlDataAdapter cmd = new SqlDataAdapter("select * from a1", con);
        DataSet ds = new DataSet();
        cmd.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
    public void binddata1()
    {
        SqlConnection con = new SqlConnection(@"data source=R4R-01\SQLEXPRESS; 
initial catalog= banneth; integrated security=true;");
        con.Open();
        SqlDataAdapter cmd = new SqlDataAdapter("select name from a1", con);
        DataSet ds = new DataSet();
        cmd.Fill(ds);
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "name";
        DropDownList1.DataBind();

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

           binddata();
           binddata1();
        }
    }
// get the values of selected row from a Gridview and display the values in textboxes using C# code.
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
        TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
        TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
        TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;
    }
//Update Row 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
      string  user = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
      string l = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
  string m = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        SqlConnection con = new SqlConnection(@"data source=R4R-01\SQLEXPRESS; 
initial catalog= banneth; integrated security=true;");
        con.Open();
        SqlCommand cmd = new SqlCommand("Update a1 set fathername='" + user + 
"' where user_id='" + s + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        binddata();
    }
//Edit Row code
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
         GridView1.EditIndex = e.NewEditIndex;
         binddata();
    }
}


Output:
previous

No comments:

Post a Comment