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


Top 5 Contributors of the Month
Jean Paul
SP
satyapriyanayak
ASPEvil
Ashutosh Jha

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind Listbox inside gridview in CSharp

Posted By:shankey       Posted Date: April 30, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article I will explain how to Bind Listbox inside gridview in asp.net website/webapplication using CSharp
 

Bind Listbox inside grid view in CSharp

Step1:  Create data table for Grid view and Listbox

For grid view control

  

For listbox control


Step2:  Bind data table to grid view control



Step3:  On RowDataBound

Event grab the listbox control from the row and bind the desired data table to the listbox

Step4: Run application

Full C# code of the above example

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
    public DataTable dtDay = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Creating static datatable
            DataTable dt = new DataTable();
            dt.Columns.Add("month");

            //Adding rows
            dt.Rows.Add("January");
            dt.Rows.Add("February");
            dt.Rows.Add("March");
            dt.Rows.Add("April");
            dt.Rows.Add("May");
            dt.Rows.Add("June");
            dt.Rows.Add("July");
            dt.Rows.Add("August");
            dt.Rows.Add("September");
            dt.Rows.Add("October");
            dt.Rows.Add("November");
            dt.Rows.Add("December");

            //Binding to grid
            gv.DataSource = dt;
            gv.DataBind();


        }


    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ListBox lstDay = (ListBox)e.Row.FindControl("lstDay");
            lstDay.DataSource = dtDay;
            lstDay.DataTextField = "Day";
            lstDay.DataValueField = "Day";
            lstDay.DataBind();

            lstDay.SelectedIndex = e.Row.RowIndex;
        }
        else if (e.Row.RowType == DataControlRowType.Header)
        {
            //Creating static datatable
            dtDay.Columns.Add("Day");
            //Adding rows
            dtDay.Rows.Add("1");
            dtDay.Rows.Add("2");
            dtDay.Rows.Add("3");
            dtDay.Rows.Add("4");
            dtDay.Rows.Add("5");
            dtDay.Rows.Add("6");
            dtDay.Rows.Add("7");
            dtDay.Rows.Add("8");
            dtDay.Rows.Add("9");
            dtDay.Rows.Add("10");
            dtDay.Rows.Add("11");
            dtDay.Rows.Add("12");
            dtDay.Rows.Add("13");
            dtDay.Rows.Add("14");
            dtDay.Rows.Add("15");
            dtDay.Rows.Add("16");
            dtDay.Rows.Add("17");
            dtDay.Rows.Add("18");
            dtDay.Rows.Add("19");
            dtDay.Rows.Add("20");
            dtDay.Rows.Add("21");
            dtDay.Rows.Add("22");
            dtDay.Rows.Add("23");
            dtDay.Rows.Add("24");
            dtDay.Rows.Add("25");
            dtDay.Rows.Add("26");
            dtDay.Rows.Add("27");
            dtDay.Rows.Add("28");
            dtDay.Rows.Add("29");
            dtDay.Rows.Add("30");
        }

    }
}

GridView control code with Listbox in TemplateField in .aspx page

 
            
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                        
                    
                
            
        

You can also download the code sample used in above articles below

 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