How to databind a dropdownlist in a datagrid

Discussion in 'ASP .Net' started by Jimmy, Jul 4, 2005.

  1. Jimmy

    Jimmy Guest

    Hi all


    I want to databind a dropdownlist in a datagrid:

    private void Page_Load(object sender, System.EventArgs e)

    {

    // Put user code to initialize the page here

    //SqlConnection con = new SqlConnection

    SqlDataAdapter da2 = new SqlDataAdapter("select * from categories",con);

    DataSet ds = new DataSet();

    ds = new DataSet();

    da2.Fill(ds);

    DataGrid2.DataSource=ds;

    DataGrid2.DataBind();

    }

    private void DataGrid2_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)

    {

    if(e.Item.ItemType == ListItemType.AlternatingItem ||

    e.Item.ItemType == ListItemType.Item)

    {

    SqlDataAdapter da2 = new SqlDataAdapter("select city from employees",con);

    DataSet ds = new DataSet();

    ds = new DataSet();

    da2.Fill(ds);

    DropDownList list = (DropDownList)e.Item.FindControl("ddl");

    list.DataSource = ds;

    list.DataBind();

    }

    }



    Page:

    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid id="DataGrid2" style="Z-INDEX: 101; LEFT: 64px; POSITION:
    absolute; TOP: 40px" runat="server"
    Height="224px" Width="392px">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:DropDownList ID="ddl" Runat="server"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </HTML>




    How can it be done?



    Ch Jimmy
    Jimmy, Jul 4, 2005
    #1
    1. Advertising

  2. Patrick Olurotimi Ige, Jul 4, 2005
    #2
    1. Advertising

  3. Patrick Olurotimi Ige schrieb:
    > Check out this article:-
    > http://www.4guysfromrolla.com/webtech/050801-1.shtml
    > Hope it helps
    > Patrick
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***


    Hi.

    I've got nearly the same question, but with more skill (I think).

    Instead of the DropDownList I want to bind a ListBox to the DataGrid.
    With help from the link posted above I realized the basics. The ListBox
    is placed into the DataGrid correctly. But now I want some ListItems to
    be selected. I can detect which items should be selected by code, but I
    cannot tell that my ListBox because the source of the ListBox is a
    DataView. And there's no possibility (I don't find any) to do this with
    a DataView.
    Allright? Can anyone help me?

    Thanks!
    Dennis Dobslaf, Jul 7, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kimberly Walker

    Dropdownlist adding data after databind

    Kimberly Walker, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    453
    Kimberly Walker
    Oct 23, 2003
  2. stefano mostarda

    DropDownList databind

    stefano mostarda, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    494
    Cowboy \(Gregory A. Beamer\)
    Nov 19, 2003
  3. Srinivas

    dropdownlist databind problems

    Srinivas, Jan 24, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,413
    niranjan19mad
    Jan 7, 2009
  4. asptodotnet
    Replies:
    1
    Views:
    391
    =?Utf-8?B?TWlrZUw=?=
    Nov 21, 2004
  5. Michael
    Replies:
    0
    Views:
    215
    Michael
    Dec 29, 2003
Loading...

Share This Page