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.
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
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."
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>"
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...)
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.
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.