Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating dropdownlist colums inside datagrid 1

Status
Not open for further replies.

nomu26

Programmer
Sep 10, 2003
22
ZA
how do i populate a dropdownlist column in a datagrid??
pls help
 
I believe that I have answered this one before - please make sure that you search for an answer. I know it is hard to find what you are looking for sometimes. Here is the code that I use to have a bound drop down list inside my datagrid.

Hope this helps.

<asp:TemplateColumn HeaderText="Expense Type">
<ItemTemplate>
<asp:Label id=lblExpTyp runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExpenseType_VC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=cboExpType runat="server" Width="130px" DataValueField="ks_KomputrolExpenseType_ID" DataTextField="ExpenseType_VC" DataSource="<%# GetExpTypeEdit() %>" SelectedIndex='<%# GetExpTypeIndex(Container.DataItem("ks_KomputrolExpenseType_ID")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Function GetExpTypeEdit() As DataSet
lblError.Text = ""

Try
Dim sb As SQLBean = New SQLBean
ds = sb.getExpType()
Catch ex As Exception
lblError.Text = Trim(lblError.Text & " A problem was found loading the Expense Types for editing. Please try again.")
End Try

Return ds

End Function

Function GetExpTypeIndex(ByVal strID As String) As Integer
Dim i As Integer
Dim dt As DataTable = ds.Tables("Returned")
For i = 0 To dt.Rows.Count - 1
If Int32.Parse(strID) = Int32.Parse(dt.Rows(i)("ks_KomputrolExpenseType_ID")) Then
Return i
End If
Next i
End Function

Public Function getExpType() As DataSet
Try
Dim strSQL As String = "SELECT ks_KomputrolExpenseType_ID, ExpenseType_VC FROM dbo.ks_KomputrolExpenseType_T ORDER BY ExpenseType_VC"

conn = New SqlConnection(strConn)
Dim cmd As SqlCommand
cmd = New SqlCommand(strSQL, conn)

Dim da As SqlDataAdapter = New SqlDataAdapter
da.SelectCommand = cmd
da.Fill(ds, "Returned")
Catch ex As Exception
Finally
If Not conn Is Nothing Then
conn.Close()
End If
End Try

Return ds
End Function

Hope everyone is having a great day!

Thanks - Jennifer
 
thanks for the response Jennifer but can you look at my code below and tell me what I'm doing wrong cause the error I'm getting is:
"DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name Status."

<asp:TemplateColumn HeaderText="Status">
<HeaderStyle Font-Bold="True" Font-Size="XX-Small" Width="60" Font-Name="verdana" Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Font-Size="XX-Small" Font-Names="Arial"></ItemStyle>
<ItemTemplate>
<asp:Label id="Label3" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>' Visible="False" Font-Bold="True">
</asp:Label>
<asp:DropDownList ID="cboStatus" Runat="server"></asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>


Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
Dim dsPageData As New DataSet
Dim strSQL As String
If e.Item.ItemType = ListItemType.Item Then
Dim ddl As DropDownList = CType(e.Item.FindControl("cboStatus"), DropDownList)
Dim dView As New DataView
dView.Table = dsPageData.Tables("tblStatus")
dView.RowFilter = "Status = '" & e.Item.Cells(2).Text & "'"
ddl.DataSource = dsPageData.Tables("tblStatus")
ddl.DataTextField = "Status"
ddl.DataValueField = "StatusID"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("--Select Status--"))
End If
End Sub

can you also explain what this label means:
"<asp:Label id=lblExpTyp runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExpenseType_VC") %>'>
</asp:Label>"
 
NOMU26,

The Last Part of your Question: In the Item Template (not in edit mode) I show a label with the text needed. It is bound to the Description of the Expense Type in this instance.

Code Review:

First of all your Drop Down is in the Item Template instead of the Edit Template. Also you need to bind the data source and value in the HTML. See my code above. You don't need a drop down unless you are in edit mode, just use the label. You are replacing the TextBox with the DropDownList (sometimes I will even use both but that is another story...)

<asp:TemplateColumn HeaderText="Status">
<HeaderStyle Font-Bold="True" Font-Size="XX-Small" Width="60" Font-Name="verdana" Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Font-Size="XX-Small" Font-Names="Arial"></ItemStyle>
<ItemTemplate>
<asp:Label id="Label3" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>' Visible="False" Font-Bold="True">
</asp:Label>
<asp:DropDownList ID="cboStatus" Runat="server"></asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>

I don't normally fill the dropdownlist on the ItemBound event. The following would be how I set a listbox based on the entry in the text field.

<asp:TemplateColumn HeaderText="Client / City / State Notation">
<ItemTemplate>
<asp:Label id=lblClientCityState runat="server" Width="338px" Text='<%# DataBinder.Eval(Container, "DataItem.ClientCityStateNotation_VC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtClientCityStateEdit runat="server" Width="338px" Font-Size="10px" Font-Names="Arial" AutoPostBack="True" Text='<%# DataBinder.Eval(Container, "DataItem.ClientCityStateNotation_VC") %>' OnTextChanged="GetCustNameFiltered">
</asp:TextBox><BR>
<asp:ListBox id="lstClientCityStateEdit" runat="server" Width="338px" AutoPostBack="True" DataValueField="CustomerName_VC"
DataTextField="CustomerName_VC" OnSelectedIndexChanged="SetTextField" Rows="4"></asp:ListBox>
</EditItemTemplate>
</asp:TemplateColumn>

Sub GetCustNameFiltered(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim strName As String
strName = CType(Me.dgDetail.Items.Item(dgDetail.EditItemIndex).FindControl("txtClientCityStateEdit"), TextBox).Text
Dim sb As SQLBean = New SQLBean
Dim strSQL As String
If strName = "" Then
strSQL = "SELECT DISTINCT TOP 100 PERCENT CustomerName_VC FROM dbo.ks_Customer_T ORDER BY CustomerName_VC"
Else
strSQL = "SELECT DISTINCT TOP 100 PERCENT CustomerName_VC FROM dbo.ks_Customer_T WHERE CustomerName_VC LIKE '%" & strName & "%' ORDER BY CustomerName_VC"
End If
ds = sb.getClients(strSQL)
Dim lstClient As ListBox
lstClient = CType(Me.dgDetail.Items.Item(dgDetail.EditItemIndex).FindControl("lstClientCityStateEdit"), ListBox)
lstClient.DataSource = ds.Tables(0).DefaultView
lstClient.DataBind()
Catch ex As Exception
lblError.Text = Trim(lblError.Text & " A problem was found loading the Customer List for Editing. Please try again.")
End Try

End Sub

Sub SetTextField(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim lstClient As ListBox
lstClient = CType(Me.dgDetail.Items.Item(dgDetail.EditItemIndex).FindControl("lstClientCityStateEdit"), ListBox)
Dim txtClient As TextBox
txtClient = CType(Me.dgDetail.Items.Item(dgDetail.EditItemIndex).FindControl("txtClientCityStateEdit"), TextBox)
txtClient.Text = lstClient.SelectedItem.Text
End Sub


I hope this helps and points you into the right direction.


Hope everyone is having a great day!

Thanks - Jennifer
 
thank you so much again Jennifer,
but I still have a problem and now I'm not sure if I'm explaining this thing correctly I did every steps you showed me, what's happening is the Datagrid I'm creating all the fields come from one table except one field called 'Status' of which I'm trying to populate the dropdownlist with the values from tblStatus table and on this line here "<asp:Label id="Label3" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>' Visible="False" Font-bold="true" </asp:Label>" is where I get the following error "DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name Status. " in this case Status is the DataTextField.

tell me where I'm doing wrong and sorry to be a nag
 
NOMU26,

The dataset used to bind the datagrid would need to have the Status Key or Status that is selected.

The DropDownList can be bound to a different dataset but the DataBinder.Eval must point to something in the datagrid's data source.

Hope this makes sense.

Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top