Dropdownlist in a GridView

csharp , db , xhtml Kākināda, India
  • 13 years ago

    Hi,

    I have a dropdownlist in one column of a gridview.

    whenever I select a value from the dropdownlist, a usercontrol has to appear below the dropdownlist in the same row same column(where the dropdownlist is present).

    Based on my database,my gridview has three rows and three columns.Among them,one column has dropdownlist.

    my problem is whenever I select a value of dropdownlist in first row ,i am getting the usercontrol below the dropdownlist.But when I select the value from the dropdownlist of second row,the first row usercontrol is disappearing and i am getting it in the second row.

    But i need the usercontrol to appear in the first row as well as in the second row when I select a value without disappearing in the first.

    Thanks for any help.......

  • 13 years ago
    hi angarika,

      1.   Here you can take separate user controls for your columns.
      2.   You can also create diffent instances for the same user control. By taking single instance of the user control you cant displayed it at 2 places. you have to create 2nd instance for the 2nd column.

    If you require more help than send the code.






  • 13 years ago

    Hi Manan_shah,

    I am new to asp.net.so plz edit this code.....so that I can understand it easily.

    I am sending my code....

    this is my GridView.aspx code which consists the gridview and  dropdownlistSmiley Face [:)]

    <%

    @ Page Language="C#" AutoEventWireup="true" CodeFile="GridView.aspx.cs" Inherits="GridView" EnableEventValidation="false"%>

    <!

    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 Height="160px" Width="184px" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">

    <Columns>

    <asp:BoundField DataField="Id" HeaderText="ID" />

    <asp:BoundField DataField ="Name" HeaderText="Name" />

    <asp:TemplateField HeaderText="Items">

    <ItemTemplate >

    <asp:DropDownList ID=ddlList runat=server OnSelectedIndexChanged=AppearUC AutoPostBack=true>

    <asp:ListItem Value=select>Select</asp:ListItem>

    <asp:ListItem Value=cse>CSE</asp:ListItem>

    <asp:ListItem Value=ece>ECE</asp:ListItem>

    </asp:DropDownList>

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />

    <RowStyle BackColor="White" ForeColor="#003399" />

    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />

    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />

    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />

    </asp:GridView>

    </div>

    </form>

    </

    body>

    </

    html>

    this is my GridView.aspx.csSmiley Face [:)]

    using

    System;

    using

    System.Data;

    using

    System.Configuration;

    using

    System.Collections.Generic;

    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;

    using

    System.Reflection;

     

    public

    partial class GridView : System.Web.UI.Page

    {

    protected System.Web.UI.WebControls.DropDownList mySelector;

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!Page.IsPostBack)

    {

    GridView1.DataSource = GetdataSet().Tables[0];

    GridView1.DataBind();

    }

    }

    private DataSet GetdataSet()

    {

    string query = @"select * from GridSample";

    string connectionString = @"Server=mkd2-167227\sqlexpress;Database=JoSample;Trusted_Connection=Yes";

    SqlConnection myConnection = new SqlConnection(connectionString);

    SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);

    DataSet ds = new DataSet();

    ad.Fill(ds);

    return ds;

    }

    private UserControl LoadControl(string UserControlPath, params object[] constructorParameters)

    {

    List<Type> constParamTypes = new List<Type>();

    foreach (object constParam in constructorParameters)

    {

    constParamTypes.Add(constParam.GetType());

    }

    UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

    ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

    if (constructor == null)

    {

    throw new MemberAccessException("the requested constructor was not found on:" + ctl.GetType().BaseType.ToString());

    }

    else

    {

    constructor.Invoke(ctl, constructorParameters);

    }

    return ctl;

    }

    protected void AppearUC(object sender, EventArgs e)

    {

    DropDownList ddl = (DropDownList)sender;

    DataControlFieldCell cell = (DataControlFieldCell)ddl.Parent;

    if (ddl.SelectedIndex != 0)

    {

    cell.Controls.Add(LoadControl(

    "ListItem.ascx",ddl.SelectedValue));

    }

    }

    }

    This is my ListView.ascx code in which listboxes are thereSmiley Face [:)]

    <%

    @ Control Language="C#" AutoEventWireup="true" CodeFile="ListItem.ascx.cs" Inherits="ListItem"%>

    <

    html>

    <

    head>

    </

    head>

    <

    body>

    <

    form name="myform">

    <

    table border=0>

    <

    tr>

    <

    td style="height: 92px">

    <

    asp:ListBox ID="ListBox1" runat="server" Height="88px" Width="104px" SelectionMode=Multiple>

    </

    asp:ListBox>

    </

    td>

    <

    td style="height: 92px">

    <

    table align=center border=0>

    <

    tr>

    <

    td>

    <

    asp:ImageButton runat=server ID=imggt ImageUrl="~/Images/gt.gif" />

    </

    td>

    </

    tr>

    <

    tr><td></td></tr>

    <

    tr>

    <

    td>

    <

    asp:ImageButton runat=server ID=imglt ImageUrl="~/Images/lt.gif"/>

    </

    td>

    </

    tr>

    </

    table>

    </

    td>

    <

    td style="height: 92px">

    <

    asp:ListBox ID="ListBox2" runat="server" Height="88px" Width="104px" SelectionMode=Multiple></asp:ListBox>

    </

    td>

    </

    tr>

    </

    table>

    </

    form>

    </

    body>

    </

    html>

    This is my ListView.ascx.cs

    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;

    using

    System.Text;

    public

    partial class ListItem : System.Web.UI.UserControl

    {

    public ListItem()

    {

    }

    public ListItem(string ddlval)

    {

    DataSet ds = GetdataSet(ddlval);

    ListBox1.DataSource = ds.Tables[0];

    ListBox1.DataTextField = "Subject";

    ListBox1.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    private DataSet GetdataSet(string val)

    {

    string query = @"select * from ListSample where Branch='" + val + "'";

    string connectionString = @"Server=mkd2-167227\sqlexpress;Database=JoSample;Trusted_Connection=Yes";

    SqlConnection myConnection = new SqlConnection(connectionString);

    SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);

    DataSet ds = new DataSet();

    ad.Fill(ds);

    return ds;

    }

    }

     

    Apologies for dumping all the code........Thanks for any help.....

     

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Nine people can't make a baby in a month.” - Fred Brooks