Hi,
My question today is below is my code which binds a listview to a databound column in my datagrid, but at the moment the list items are hard coded in. What I actually want is for the listitems to be populated from a lookup style query so possible from a data reader bringing back the ID which will be stored in the bound column but so the user just sees the Text associated with the value. Hope that makes sense.
<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropModel" Runat="server">
<asp:ListItem Value="1" text="1"></asp:ListItem>
<asp:ListItem Value="2" text="2"></asp:ListItem>
<asp:ListItem Value="3" text="3"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="DropModel" text="*" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
I think I can pretty much suss this out if I can work out how to access the dropdown list from tthe codebehind. So if anyone knows that as a starting point, please enlighten me.
PS sorry about the smilies in the code the ASP syntax just happent to be the same code.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp
Ok I found this code at the above link but Im struggling to use it, can anyone explain it line by line or tell me how to make it reference my template item shown above.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim Cu As String = DRV("ShipVia")
Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList)()
Dim SQL As String = _
"SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub
Yes, you'd do it in the ItemDataBound event. After checking for the ItemType, declare a variable of type Combobox. Set it to e.Item.Cells(x).FindControl(your combobox id), and set the value of that combobox to the value you want from the dataset. You can use e.Item.ItemIndex to get the row number in the repeater/grid/dataset.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim cboModel As DropDownList
Dim cboModel As DropDownList = e.Item.Cells(FindControl(DropModel))
'Also I have put DopModel as the ID of the Dropdown list in the item template but it doesnt seem to be recognised? Name DropModel is not declared
End If
End Sub
Fixed
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")
Now to try the next bit...
Nearly there, I am now just getting an error at run time on the line underlined saying cannot convert dbnull to string?
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")
Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)
If Not lstItem Is Nothing Then lstItem.Selected = True
End If
End Sub
DataBinder.Eval(e.Item.DataItem, "FK_StockModelID")
Like this ?
If e.Item.ItemType = ListItemType.EditItem Then
Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DataBinder.Eval(e.Item.DataItem, "FK_StockModelID") 'DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")
Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)
If Not lstItem Is Nothing Then lstItem.Selected = True
End If
I still get same error
Hmm... maybe before you call that line, you should check whether e.Item.DataItem evaluates to null... place a breakpoint there and have a look at it.
It evaluates to a system.data.datarowview object
Hi Mendhak, I have finally solved the problem a different way.
My HTML code looks like this now, the main difference is I have set the datasource to a function in my code behind page.
<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=DropModel Runat="server" DataValueField="ModelID" DataTextField="BrandModel" DataSource="<%# PopulateList %>" >
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" Runat="server" text="*" ControlToValidate="DropModel"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>
The function simply returns a previously filled dataset.
Public Function PopulateList() As DataSet
Return Main.objCarriageBuildSheet.dds_StockModels
End Function
this seems to be working nicely.
0 comments:
Post a Comment