Search
Winners

Win Prizes

Social Presence
Like us on Facebook
Advertisements
Ads

How to add dropdownlist dynamically. dynamically adding database should be bind with database. Reply me ASAP

Chandru Ganesh
Posted by Chandru Ganesh under C# on 2/28/2013 4:09:21 AM | Points: 75 | Views : 4883 | Status : [Member]


C# Coding:

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

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

    SqlConnection con = new SqlConnection("Data Source=KAARDP07\\SQLEXPRESS;Initial Catalog=sample;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
               }
    }

   
   
    protected void BindGridview()
    
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from CB1", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        gvUserInfo.DataSource = ds;
        gvUserInfo.DataBind();

    }
    
   

    protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
      
        {
            con.Open();
            var ddl = (DropDownList)e.Row.FindControl("ddlCity");
            int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
            SqlCommand cmd = new SqlCommand("select * from CB1", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds;
            ddl.DataTextField = "Number";
            ddl.DataValueField = "ID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));
        
        }
    }


    protected void ButtonAdd_Click(object sender, EventArgs e)
   
    {
        
        AddNewRowToGrid();

    }

    private void AddNewRowToGrid()
    {
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
                //add new row to DataTable
                dtCurrentTable.Rows.Add(drCurrentRow);
                //Store the current data to ViewState
                ViewState["CurrentTable"] = dtCurrentTable;
                for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
                {
                    //extract the DropDownList Selected Items
                    DropDownList ddl1 = (DropDownList)gvUserInfo.Rows[i].Cells[1].FindControl("DropDownList1");
                    //DropDownList ddl2 = (DropDownList)gvUserInfo.Rows[i].Cells[2].FindControl("DropDownList2");
                    //DropDownList ddl3 = (DropDownList)gvUserInfo.Rows[i].Cells[3].FindControl("DropDownList3");
                    // Update the DataRow with the DDL Selected Items
                    dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text;
                    //dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text;
                    //dtCurrentTable.Rows[i]["Column3"] = ddl3.SelectedItem.Text;
                }
                //Rebind the Grid with the current data
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from CB1", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                con.Close();
                gvUserInfo.DataSource = ds;
                gvUserInfo.DataBind();
            }
        }
    }
}
   
    
Aspx Code:

<%@ 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 id="Head1" runat="server">
<title>Asp.net Bind Data to Dropdownlist in inside of gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvUserInfo_RowDataBound" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:TemplateField HeaderText="Number">
<ItemTemplate>
<asp:DropDownList ID="ddlCity" runat="server" Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<footerStyle HorizontalAlign="Right" />

            <footerTemplate>

             <asp:Button ID="ButtonAdd" runat="server" Text="Click It"

                    onclick="ButtonAdd_Click" />

            </footerTemplate>
 </div>
</form>
</body>
</html>
   

                   



Comments or Responses


Login to post response