.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Gridview dropdown with related records in textbox

Posted By:satyapriyanayak       Posted Date: November 15, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

We will know how to get the related records from the database into the corresponding textboxes, which are present inside the grid view control when we click the selected item from the dropdown list, which is present inside the grid view control. Here we add the Grid View control to the web page, then add four template columns: one for the Dropdown List and three for the Textbox's. Set the AutoPostBack property to "True" for the Dropdown List, and create the SelectedIndexChanged even
 

The Default. aspx code is given below

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
   OnRowDataBound="GridView1_RowDataBound">

<Columns>
<asp:TemplateField HeaderText="EmployeeID">
<ItemTemplate>
<asp:DropDownList Width="100" runat="server"
   id="ddlTest" AutoPostBack="true"
   OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:TextBox ID="txt3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
    </div>
    </form>
</body>
</html>

The Default. aspx.cs code is given below

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
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlDataAdapter sqlda = new SqlDataAdapter();
    SqlCommand com = new SqlCommand();
    DataTable dt;
    string str;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindgrid();
        }
    }
    private void bindgrid()
    {
        SqlConnection conn = new SqlConnection(connStr);
        dt = new DataTable();
        com.Connection = conn;
        com.CommandText = "SELECT * FROM Employees";
        sqlda = new SqlDataAdapter(com);
        sqlda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Control ctrl = e.Row.FindControl("ddlTest");
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
            
                SqlConnection conn = new SqlConnection(connStr);
                dt = new DataTable();
                com.Connection = conn;
                com.CommandText = "SELECT * FROM Employees";
                sqlda = new SqlDataAdapter(com);
                sqlda.Fill(dt);
                dd.DataTextField = "EmployeeID";
                dd.DataValueField = "EmployeeID";
                dd.DataSource = dt;
                dd.DataBind();
            }
        }

    }
    protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        DropDownList ddl = sender as DropDownList;
       
        foreach (GridViewRow row in GridView1.Rows)
        {
            
        Control ctrl = row.FindControl("ddlTest") as DropDownList;
        if (ctrl != null)
         {
          DropDownList ddl1 = (DropDownList)ctrl;

          if (ddl.ClientID == ddl1.ClientID)
           {
             TextBox txt1 = row.FindControl("txt1") as TextBox;
             TextBox txt2 = row.FindControl("txt2") as TextBox;
             TextBox txt3 = row.FindControl("txt3") as TextBox;
             SqlConnection conn = new SqlConnection(connStr);
             conn.Open();
             str = "select * from Employees where EmployeeID='" + ddl1.SelectedItem.Text + "'";
             com = new SqlCommand(str, conn);
            SqlDataReader reader = com.ExecuteReader();
        while (reader.Read())
        {
            txt1.Text = reader["FirstName"].ToString();
            txt2.Text = reader["LastName"].ToString();
            txt3.Text = reader["Address"].ToString();

        }
        reader.Close();
        conn.Close();
        }       
        }
        }
        }
    }



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend