This is a migrated thread and some comments may be shown as answers.

Multi-Column databinding in webpart

1 Answer 88 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Mark Ferkaluk
Top achievements
Rank 1
Mark Ferkaluk asked on 18 Jan 2010, 08:35 AM
Hello everyone,

I'm currently trying to use RadControls in my Sharepoint WebPart. One of it is the ComboBox control.
I wanted to know, how I can bind my ComboBox with multiple columns to a SPDataSource.

I already accomplished to bind the first comlumn, but I don't know how to bind the others.

First I have my HeaderTemplate here:

Class HeaderTemplate
    Implements ITemplate

    Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        Dim table As New HtmlTable()
        Dim row As New HtmlTableRow()
        Dim cell As New HtmlTableCell()

        table.Width = "100%"

        cell.InnerHtml = "<font size='1pt'><b>Projekt Nr.</b></font>"
        cell.Width = "30%"
        row.Controls.Add(cell)

        Dim cell1 As New HtmlTableCell()

        cell1.InnerHtml = "<font size='1pt'><b>Projekt Name</b></font>"
        cell1.Width = "70%"
        row.Controls.Add(cell1)

        table.Controls.Add(row)

        container.Controls.Add(table)
    End Sub
End Class

And here I bind data:

Dim ds As New SPDataSource
ds.DataSourceMode = SPDataSourceMode.List
ds.List = web.Lists("Projekte")
ds.UseInternalName = True

projekt = New RadComboBox
projekt.ID = "cmb_projekt"
projekt.Width = 300
projekt.Skin = "WebBlue"

projekt.HeaderTemplate = New HeaderTemplate()

projekt.DataSource = ds
projekt.DataTextField = "Nr"
projekt.DataBind()

Thanks for all replies.

Best regards,

Mark

1 Answer, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 21 Jan 2010, 10:57 AM
Hello Mark Ferkaluk,

Here is one example that illustrates how to bind RadComboBox with multiple columns to a SqlDataSource, I believe that the case with SPDataSource is similar.

You are on the right way using HeaderTemplate in your code.

In my example I added an ItemTemplate class and one more column that I bound to a custom attribute (in case you need to display more than two columns in the future). 

Partial Class VB_Examples_SharepointRadCombo
    Inherits System.Web.UI.Page
  
    Private projectHeader As HeaderTemplateSharepoint
    Private projectItems As ItemTemplateSharepoint
  
    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)
  
        projectHeader = New HeaderTemplateSharepoint()
        projectHeader.InstantiateIn(New PlaceHolder())
        productsCombo.HeaderTemplate = projectHeader
  
        projectItems = New ItemTemplateSharepoint()
        projectItems.InstantiateIn(New PlaceHolder())
        productsCombo.ItemTemplate = projectItems
  
    End Sub
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
  
    Protected Sub productsCombo_ItemsRequested(ByVal o As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
  
        Dim s As New SqlDataSource()
        s.ID = "mySqlSourceControl"
        Page.Controls.Add(s)
  
        s.ConnectionString = ConfigurationManager.ConnectionStrings("NorthwindConnectionString1").ConnectionString
        s.SelectCommand = "SELECT [ProductID], [ProductName], [UnitPrice] from [Products]"
        Dim args As New DataSourceSelectArguments()
  
        Dim dv As DataView = DirectCast(s.[Select](args), DataView)
        Dim dt As DataTable = dv.ToTable()
        For Each dataRow As DataRow In dt.Rows
            Dim item As New RadComboBoxItem()
            item.Text = DirectCast(dataRow("ProductName"), String)
            item.Value = dataRow("ProductID").ToString()
  
            Dim unitPrice As Decimal = CDec(dataRow("UnitPrice"))
            item.Attributes.Add("UnitPrice", unitPrice.ToString())
  
            projectItems.ProjectTitle = item.Text
            projectItems.ProjectID = item.Value
            projectItems.ProjectPrice = unitPrice.ToString()
  
            productsCombo.Items.Add(item)
            item.DataBind()
        Next
    End Sub
  
End Class
  
Public Class ItemTemplateSharepoint
    Implements ITemplate
    Private m_projectTitle As String
    Private m_projectID As String
    Private m_projectPrice As String
  
    Public Property ProjectTitle() As String
        Get
            Return m_projectTitle
        End Get
        Set(ByVal value As String)
            m_projectTitle = value
        End Set
    End Property
    Public Property ProjectID() As String
        Get
            Return m_projectID
        End Get
        Set(ByVal value As String)
            m_projectID = value
        End Set
    End Property
    Public Property ProjectPrice() As String
        Get
            Return m_projectPrice
        End Get
        Set(ByVal value As String)
            m_projectPrice = value
        End Set
    End Property
  
    Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        Dim table As New HtmlTable()
        table.Width = "100%"
        Dim row As New HtmlTableRow()
  
        Dim cell As New HtmlTableCell()
        cell.InnerHtml = Me.ProjectID
        cell.Width = "30%"
        row.Controls.Add(cell)
  
        Dim cell1 As New HtmlTableCell()
        cell1.InnerHtml = Me.ProjectTitle
        cell1.Width = "50%"
        row.Controls.Add(cell1)
  
        Dim cell2 As New HtmlTableCell()
        cell2.InnerHtml = Me.ProjectPrice
        cell2.Width = "20%"
        row.Controls.Add(cell2)
  
        table.Controls.Add(row)
        container.Controls.Add(table)
    End Sub
  
End Class
  
  
Public Class HeaderTemplateSharepoint
    Implements ITemplate
  
    Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        Dim table As New HtmlTable()
        table.Width = "100%"
  
        Dim row As New HtmlTableRow()
  
        Dim cell As New HtmlTableCell()
        cell.InnerHtml = "<font size='1pt'><b>Projekt Nr.</b></font>"
        cell.Width = "30%"
        row.Controls.Add(cell)
  
        Dim cell1 As New HtmlTableCell()
        cell1.InnerHtml = "<font size='1pt'><b>Projekt Name</b></font>"
        cell1.Width = "50%"
        row.Controls.Add(cell1)
  
        Dim cell2 As New HtmlTableCell()
        cell2.InnerHtml = "<font size='1pt'><b>Price</b></font>"
        cell2.Width = "20%"
        row.Controls.Add(cell2)
  
        table.Controls.Add(row)
        container.Controls.Add(table)
  
    End Sub
End Class

<telerik:RadComboBox ID="productsCombo"
    EnableLoadOnDemand="true"
    OnItemsRequested="productsCombo_ItemsRequested"
    runat="server" DropDownWidth="298px">
</telerik:RadComboBox>

Best wishes,
Kalina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
ComboBox
Asked by
Mark Ferkaluk
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Share this question
or