Showing posts with label manually. Show all posts
Showing posts with label manually. Show all posts

Wednesday, March 21, 2012

Populating a DropDownList

Is there a way to manually insert rows into a dropdownlist.
I nneed to insert a value and a display text.Yes, you may use " DropDownList.Items.Add" or "Items.Insert".
How do you add a value and a display text?
ddlb.Add("50","New York") does not work. It appears to only take one argument.
OK. Do it like this:

Dim newItem As ListItem = New ListItem("New York", "50")
ddlb.Items.Add(newItem)

This will add a new item at bottom of your DDL, if you want add it in a certain palce, you amy do like this:

ddlb.Items.Insert(0,newItem)

This will add it in the very top.

Friday, March 16, 2012

Populating drop down list from database AND manually

I have a drop down list which ive popukated from my SQL Server database. However, I want to manually add another item to the list, as it doesnt appear in my DB query.

How do I do this ??

thanks
ferkh10You can use the "insert()" methods of the item array in the drop down list.

If you want to place it after all your values, just do "Insert()".

example:

dropdown.Items.Insert(new ListItem("Text", "value"))

If you want to place it in a particular place, do

dropdown.Items.Insert(IndexToPutItem, new ListItem("Text", "value"))

Note that code is in C#

Hope this helps,
Covo
ddl.Items.Insert(0,New ListItem("text","value"))
My DDL is created in an EditItemTemplate, and I dont seem to be able to reference it from my code behind. Heres where i created the DDL :


<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlAccountNames" DataValueField="intMTIID" DataTextField="vchAccount" DataSource='<%# GetAccountNames() %>' SelectedIndex='<%# GetSelectedIndex(Container.DataItem("intMTIID")) %>' >
</asp:DropDownList>
</EditItemTemplate>

I populate the DDL using the code below :


Function GetAccountNames() As DataSet
'Populate the ddlDataSet

Const strConnStr As String = "XXXXXXXXXXXX"
Dim myConnection As New Data.SqlClient.SqlConnection(strConnStr)

Const strSQL = "spGetMarketTraderAccounts "
Dim myDataAdapter As Data.SqlClient.SqlDataAdapter = New Data.SqlClient.SqlDataAdapter(strSQL, myConnection)
myDataAdapter.Fill(ddlDataSet, "Accounts")

Return ddlDataSet
End Function

Whereabouts do I need to put the InsertAt statement ? Ive tried putting it into the GetAccountNames function, but its not recognising the DDL name.
If it is in edit item you can reference it in codebehind from DataGrid's ItemDataBound event handler (you get the correct item by checking when item is of type EditItem, from event handler's event arguments)
Im still having real problems with this. Can anybody post a code example that I can use ??

thanks.
Your grid could be like this:


<asp:DataGrid ID="ExampleGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" EditText="Edit" CancelText="Cancel" />
<asp:TemplateColumn>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"SelectedField")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlAccountNames" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="SomeText" ReadOnly="True" />
</Columns>
</asp:DataGrid>

Then the code:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
'Bind DataGrid to dummy data
Bindgrid()
End If
End Sub

'Bind the grid itself using dummy data
Private Sub Bindgrid()
Dim dt As New DataTable
dt.Columns.Add("SelectedField", GetType(Integer))
dt.Columns.Add("SomeText", GetType(String))

Dim dr As DataRow = dt.NewRow()
dr(0) = 1
dr(1) = "One thing here"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 2
dr(1) = "Two things there"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 3
dr(1) = "Three things anywhere"
dt.Rows.Add(dr)

ExampleGrid.DataSource = dt
ExampleGrid.DataBind()
End Sub

Private Sub ExampleGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles ExampleGrid.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
'LOcate the DDL in the edit item
Dim ddl As DropDownList = CType(e.Item.FindControl("ddlAccountNames"), DropDownList)
ddl.DataSource = GetAccountNames()
ddl.DataValueField = "Field"
ddl.DataTextField = "Text"
ddl.DataBind()

'Search for the item in grid's data source and put it as seleted
Dim ditem As DataRowView = CType(e.Item.DataItem, DataRowView)
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(ditem("SelectedField")))
End If
End Sub

Private Function GetAccountNames() As DataSet
'Using dummy data here for ddl populating
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("Field", GetType(Integer))
dt.Columns.Add("Text", GetType(String))

Dim dr As DataRow = dt.NewRow()
dr(0) = 1
dr(1) = "One"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 2
dr(1) = "Two"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 3
dr(1) = "Three"
dt.Rows.Add(dr)

Return ds
End Function

Private Sub ExampleGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ExampleGrid.EditCommand
ExampleGrid.EditItemIndex = e.Item.ItemIndex
Bindgrid()
End Sub

Private Sub ExampleGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ExampleGrid.CancelCommand
ExampleGrid.EditItemIndex = -1
Bindgrid()
End Sub

This example doesn't show how to pass changes in DDL to the database, but that shouldn't be big deal either.