How to databind a dropdownlist in a datagrid


J

Jimmy

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
 
Ad

Advertisements

Ad

Advertisements

D

Dennis Dobslaf

Patrick said:
Check out this article:-
http://www.4guysfromrolla.com/webtech/050801-1.shtml
Hope it helps
Patrick

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top