.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 >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Repopulate Grid-Using DropDownList in Asp.net : Practical view

Posted By:Gaurav Arora       Posted Date: April 28, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Histroy: I have posted this Code-Snippet as per the requirement of Mr. Rahul Jain. Actually, he wants to repopulate-datagrid as per selected value of DropDownList.

Scope: The scope is very limited to its title and as explained between.

Introduction: On Design page Pick and drop a DataGrid, a Label and a Button. You can copy paste the following code in your newly added 'aspx' page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridWithDDL.aspx.cs"
    Inherits="DataGridWithDDL" %>




    DataGrid With DropDownList


   
       


                            EditItemStyle-BorderColor="Aqua" EditItemStyle-BackColor="AliceBlue" HeaderStyle-BackColor="greenYellow"
                OnItemDataBound="dgWithDDL_ItemDataBound">
               
                   
                        Select Choice
                       
                                                            OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">
                           

                       

                   

                   
                    
                   
                       
                            Dep. Name

                       
                            <%# DataBinder.Eval(Container.DataItem,"DpName" )%>
                       

                   

                   
                       
                            Country

                       
                            <%#DataBinder.Eval(Container.DataItem, "Country")%>
                       

                   

                    
                       
                            In House(Yes/No)

                       
                           
                       

                   

               

           

           
           

           

   



Code Behind page: The following is the Code-behind page for above aspx page.

/* This Example is a part of different
 * examples shown in Book:
 * C#2005 Beginners: A Step Ahead
 * Written by: Gaurav Arora
 * Reach at : gaurav.aroraose@yahoo.co.in
 * File Name : DataGridWithDDL.aspx.cs
 * Web : http://www.msdotnetheaven.com */

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

public partial class DataGridWithDDL : System.Web.UI.Page
{
    SqlConnection myCon;
    string str = ConfigurationManager.ConnectionStrings["conMast"].ToString();   
    DataSet myDSet;
    string strquerry;// = "Select * from HR_MAST_DEPT";
    void BindDataGrid(String strquerry)
    {
        myCon = new SqlConnection(str);
        if(strquerry == null)
             strquerry = "Select * from HR_MAST_DEPT";
        SqlDataAdapter myDa = new SqlDataAdapter(strquerry, myCon);
         myDSet = new DataSet();
        myDa.Fill(myDSet, "HR_MAST_DEPT");

        dgWithDDL.DataSource = myDSet;
        dgWithDDL.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataGrid(null);
        }
    }   
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;

        TableCell cell = list.Parent as TableCell;
        DataGridItem item = cell.Parent as DataGridItem;

        int index = item.ItemIndex;
        string content = item.Cells[1].Text;
        string content1 = list.SelectedValue.ToString();

        //This will display the content
       
        Label2.Text = "Old Value(s): "+String.Format("Row {0} contains {1}", index, content);
        Label2.Text += "
Current Value(s): " + String.Format("Row {0} contains {1}", index, content1);
        Label2.Visible = true;

        //ReBind the Grid as per selection
        strquerry = "Select * from HR_MAST_DEPT Where city = '" + content1 + "'";
        BindDataGrid(strquerry);

    }
    protected void dgWithDDL_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
           
        {
            String[] option ={ "Select City","Delhi", "Gurgaon","Chandigarh", "Guwahati" };
            DropDownList list = (DropDownList)e.Item.FindControl("ddlCity");
            list.DataSource = option;
            list.DataBind();
        }
    }
    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        //Display all record(s)
        Label2.Visible = false;
        BindDataGrid(null);
    }


}



Short Description of Code : The above SelectedIndex event just get the Selected value and rebind the datagrid. Its just a fun as you simply bind the datagrid.

Table and Database(s): Following is the Sql which created and populated Database and table(s), I have used in above snippet.


/* This query is created a HRnPAYROLL DATABASE
 * for use of different examples shown in
 * Book: C#2005 Beginners: A Step Ahead
 * Written by: Gaurav Arora */

--First Create HRnPAYROLL DATABASE
Create Database HRnPAYROLL
go

Use HRnPAYROLL

--Create Employees Table and fill data

Create Table Employees
(
    ID Varchar(4) Primary Key,
    FirstName Varchar(25) Default 'Gaurav',
    LastName Varchar(25) Default 'Arora',
    Age Varchar(4) Default '19'
)
go

--*****************************************************************************************************************
Insert Into Employees Values ('0001','Anil','Jain','37');
Insert Into Employees Values ('0002','Aman','Jain','35');
Insert Into Employees Values ('0003','Amanpreet','Singh','28');
Insert Into Employees Values ('0004','Anuradha','Bhatia','24');
Insert Into Employees Values ('0005','Archana','Garg','25');
Insert Into Employees Values ('0111','Babita','Gupta','33');
Insert Into Employees Values ('0112','Babban','Das','24');
Insert Into Employees Values ('0113','Babu','Ram','24');
Insert Into Employees Values ('0114','Babbu','Man','38');
Insert Into Employees Values ('0115','Balbir','Singh','24');
Insert Into Employees Values ('0201','Chaman','Lal','38');
Insert Into Employees Values ('0202','Charan','Das','37');
Insert Into Employees Values ('0203','Chander Bhan','Singh','49');
Insert Into Employees Values ('0204','Changej','Khan','24');
Insert Into Employees Values ('0205','Champak','Lal','18');
Insert Into Employees (ID,FirstName,Age) Values ( '0160',' AMIT KUMAR','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0180',' ABHISHEK MISHRA','24')
Insert Into Employees (ID,FirstName,Age) Values ( '0181',' ARUN KUMAR','23')
Insert Into Employees (ID,FirstName,Age) Values ( '0184',' ASHUTOSH BAJPAYEE','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0185',' ANIL KUMAR YADAV','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0188',' ASHOK KUMAR PATHAK','25')
Insert Into Employees (ID,FirstName,Age) Values ( '0190',' ABHISHEK KUMAR SINGH','23')
Insert Into Employees (ID,FirstName,Age) Values ( '0194',' AMIT KUMAR THAKUR','22')
Insert Into Employees (ID,FirstName,Age) Values ( '0549',' BRIJESH CHANDRA','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0558',' BRAJESH KUMAR','27')
Insert Into Employees (ID,FirstName,Age) Values ( '0560',' B.K. SINGH','31')
Insert Into Employees (ID,FirstName,Age) Values ( '0562',' BALBIR SINGH [MANGAT]','35')
Insert Into Employees (ID,FirstName,Age) Values ( '1244',' DHARMESH PANWAR','27')
Insert Into Employees (ID,FirstName,Age) Values ( '1296',' DEEPAK PRASAD','26')
Insert Into Employees (ID,FirstName,Age) Values ( '1320',' DEEPAK JAIN','30')
Insert Into Employees (ID,FirstName,Age) Values ( '2004',' FAKHRE MUBEEN','21')
Insert Into Employees (ID,FirstName,Age) Values ( '2466',' GAURAV ARORA','19')
Insert Into Employees (ID,FirstName,Age) Values ( '2480',' GURDEEP SINGH','34')
Insert Into Employees (ID,FirstName,Age) Values ( '2481',' GOPAL DATT SATI','27')
Insert Into Employees (ID,FirstName,Age) Values ( '3210',' KAHLON I.J. SINGH','26')
Insert Into Employees (ID,FirstName,Age) Values ( '3211',' INDRESH  KUMAR PANDEY','35')
Insert Into Employees (ID,FirstName,Age) Values ( '3641',' JAI PRAKASH','27')
Insert Into Employees (ID,FirstName,Age) Values ( '3683',' JAYCHANDRA','27')
Insert Into Employees (ID,FirstName,Age) Values ( '3698',' J.K. SINGH','23')
Insert Into Employees (ID,FirstName,Age) Values ( '3700',' JEET NARAYAN SINGH','29')
Insert Into Employees (ID,FirstName,Age) Values ( '3701',' JIVENDRA KUMAR','29')
Insert Into Employees (ID,FirstName,Age) Values ( '4009',' KAPOOR SINGH','30')
Insert Into Employees (ID,FirstName,Age) Values ( '4057',' KISHAN LAMA','21')
Insert Into Employees (ID,FirstName,Age) Values ( '4110',' KRISHAN KUMAR MISHRA','33')
Insert Into Employees (ID,FirstName,Age) Values ( '4112',' K.B. CHOUBEY','33')
Insert Into Employees (ID,FirstName,Age) Values ( '4113',' KAISH UDDIN','35')
Insert Into Employees (ID,FirstName,Age) Values ( '4455',' LOKNATH BALBANTARAY','34')
Insert Into Employees (ID,FirstName,Age) Values ( '4801',' M.K. DEBROY','30')
Insert Into Employees (ID,FirstName,Age) Values ( '4884',' MADAN GIRI','21')
Insert Into Employees (ID,FirstName,Age) Values ( '4936',' MANJISH KUMAR','34')
Insert Into Employees (ID,FirstName,Age) Values ( '4946',' MADAN SINGH','24')
Insert Into Employees (ID,FirstName,Age) Values ( '4949',' MANOJ KUMAR TIWARI','21')
Insert Into Employees (ID,FirstName,Age) Values ( '4978',' MUKHTAR AHMED','18')
Insert Into Employees (ID,FirstName,Age) Values ( '4985',' MANOJ SAXENA','31')
Insert Into Employees (ID,FirstName,Age) Values ( '4987',' MANISH MISHRA','31')
Insert Into Employees (ID,FirstName,Age) Values ( '5263',' NEERAJ MAHAWAR','33')
Insert Into Employees (ID,FirstName,Age) Values ( '5271',' NIRAJ KUMAR SINHA','19')
Insert Into Employees (ID,FirstName,Age) Values ( '5272',' N.K. SINGH','21')
Insert Into Employees (ID,FirstName,Age) Values ( '5274',' NITIN GUPTA','32')
Insert Into Employees (ID,FirstName,Age) Values ( '5275',' NARESH KUMAR GUPTA','23')
Insert Into Employees (ID,FirstName,Age) Values ( '5612',' O.P. YADAV','32')
Insert Into Employees (ID,FirstName,Age) Values ( '6135',' PRAMOD SINGH RAWAT','30')
Insert Into Employees (ID,FirstName,Age) Values ( '6136',' PRAMOD KUMAR GAUTAM','22')
Insert Into Employees (ID,FirstName,Age) Values ( '6137',' PRAMOD KUMAR SINHA','31')
Insert Into Employees (ID,FirstName,Age) Values ( '6138',' PRASAD SASNUR','20')
Insert Into Employees (ID,FirstName,Age) Values ( '6700',' R. BHATTACHARJEE','21')
Insert Into Employees (ID,FirstName,Age) Values ( '6753',' RAMA SHANKAR','20')
Insert Into Employees (ID,FirstName,Age) Values ( '6765',' RAMESH CHANDRA','27')
Insert Into Employees (ID,FirstName,Age) Values ( '6776',' RAMU YADAV','20')
Insert Into Employees (ID,FirstName,Age) Values ( '6899',' RAVINDER LAMBA','29')
Insert Into Employees (ID,FirstName,Age) Values ( '6925',' RAVINDRA KUMAR RANA','24')
Insert Into Employees (ID,FirstName,Age) Values ( '6996',' R.P. YADAV','28')
Insert Into Employees (ID,FirstName,Age) Values ( '7010',' RAJESH PUNDIR','33')
Insert Into Employees (ID,FirstName,Age) Values ( '7011',' RAJIV CHAUDHARY','32')
Insert Into Employees (ID,FirstName,Age) Values ( '7012',' RAVINDRA YADAV','23')
Insert Into Employees (ID,FirstName,Age) Values ( '7013',' RAJESH SHARMA','32')
Insert Into Employees (ID,FirstName,Age) Values ( '7016',' RAVI SHANKAR','19')
Insert Into Employees (ID,FirstName,Age) Values ( '7017',' RAM BILAS CHOUDHARY','23')
Insert Into Employees (ID,FirstName,Age) Values ( '7022',' RAJ KISHORE RAI','24')
Insert Into Employees (ID,FirstName,Age) Values ( '7023',' R. RAMESH','35')
Insert Into Employees (ID,FirstName,Age) Values ( '7123',' SATISH KUMAR','32')
Insert Into Employees (ID,FirstName,Age) Values ( '7296',' SHANKAR LAL','29')
Insert Into Employees (ID,FirstName,Age) Values ( '7471',' SANJEEVAN M.K.','30')
Insert Into Employees (ID,FirstName,Age) Values ( '7480',' SANDEEP KR. SHRIVASTA','34')
Insert Into Employees (ID,FirstName,Age) Values ( '7484',' SUBHARAM BANERJEE','25')
Insert Into Employees (ID,FirstName,Age) Values ( '7490',' S.M. TRIPATHI','31')
Insert Into Employees (ID,FirstName,Age) Values ( '7501',' TEK BAHADUR CHATTRI','23')
Insert Into Employees (ID,FirstName,Age) Values ( '7513',' TULSI PRASAD','30')
Insert Into Employees (ID,FirstName,Age) Values ( '7919',' UMESH CHANDRA MISHRA','19')
Insert Into Employees (ID,FirstName,Age) Values ( '8480',' VIVEK TRIPATHI','31')
Insert Into Employees (ID,FirstName,Age) Values ( '8481',' VIJAY KUMAR','22')
Insert Into Employees (ID,FirstName,Age) Values ( '8485',' VIJAY KUMAR MISHRA','20')
Insert Into Employees (ID,FirstName,Age) Values ( '8486',' VINEET KUMAR SAXENA','35')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J001',' ANURAG', 'MISHRA','21')
--*****************************************************************************************************************
-- Create HR_MAST_DEPT Table

CREATE TABLE HR_MAST_DEPT
(
   

 [char] (2) Primary Key ,
    [Dpname] [varchar] (25) NOT NULL ,
    [city] [varchar] (25),
    [country] [varchar] (50),
    [inhouse] [bit],

)
gO

-- Fill HR_MAST_DEPT Table

Insert Into HR_MAST_DEPT Values ( '10' , 'Information Technology' , 'Delhi' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '20' , 'Human Resources Dept' , 'Mohali' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '30' , 'Marketing' , 'Toronto' , 'Canada' , '0' )
Insert Into HR_MAST_DEPT Values ( '40' , 'Civil' , 'Calgiri' , 'Canada' , '0' )
Insert Into HR_MAST_DEPT Values ( '50' , 'Sales' , 'Gurgaon' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '60' , 'Commercial' , 'Delhi' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '70' , 'Admin' , 'Noida' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '80' , 'Testing' , 'Gurgaon' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '90' , 'Implementing' , 'Delhi' , 'India' , '1' )

--*****************************************************************************************************************



Web.Config file:  The following is the web.config file. I have used the connections string there.




   
 
       name="conMast"
   connectionString="Server=(local);uid=sa;pwd=gaurav;database=HRnPAYROLL"
   providerName="System.Data.SqlClient"
   />

 

   
          
          
          
          
          
   

Point(s) to be noted:

  1. The scope of above snippet is upto its explanation./li>
  2. I have used direct querry in the example, but this is not the secure way.
  3. Always use Stored procedure to populate the data, its more secure.
  4. Step(s) are very easy to use above:

    • Open Visual Studio
    • Add new Web Page
    • Rewrite above code or just Copy/paste the same
    • Run by pressing F5
    • Select your options and check the populated result(s)

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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