views:

1019

answers:

2

I am looking for a vb.net example of how to bind a dataset/datatable to data repeater and have the data elements bound to the columns of the dataset/datatable? Thanks

+1  A: 

At first I thought you wanted a web repeater, but from your comments I realized you meant Microsoft.VisualBasic.PowerPacks.DataRepeater.

I need some more info from you to give the most helpful sample code (see below).

The basic steps of using a DataRepeater are:
1) Install the Visual Basic Power Packs 3 Link
2) Open a VB.net Winforms project and drag a DataRepeater to your form
3) Add a new Dataset to your project via Add->New Item menu
4) In the design window, set up columns as desired
5) Open the Data Sources window from Data->ShowDataSources menu
6) With your form in design mode, go to the Dataset in the Data Sources window and use the dropdown box next to the table name to select "Details"
7) Drag the table to top section of the DataRepeater control (on your form). The table fields should now be listed on your DataRepeater. You can move them around.
8) At runtime, you can load the Datset and the DataRepeater will automatically reflect the data changes. No .Databind is required
ex.

me.DataSet1.Tables(0).Columns.Add(New String() {"John", "Doe", "Accountant"}  

or

myDataAdapter.Fill(me.DataSet1.Tables(0)) 

Do you get tripped up on any of those steps?


Edit:

From what I have seen, it looks like DataRepeater is meant to be used in situations were you add/map the datatable to the DataRepeater at design-time. Then all you have to do is fill the datatable at run-time and the DataReader shows the data automatically.

If you really want to add the table/controls to the DataRepeater at run time, here's an example I coded for you. It assumes a form named Form3 with a DataRepeater named DataRepeater1 and a button named Button1...

Public Class Form3

    ''Set up demo DataSet/DataTable
    Const FRUIT_COL As String = "Fruit"
    Const COLOR_COL As String = "Color"
    MyDataSet = New DataSet
    MyDataSet.Tables.Add("MyTable")
    With MyDataSet.Tables(0)
        .Columns.Add(FRUIT_COL, GetType(System.String))
        .Columns.Add(COLOR_COL, GetType(System.String))
    End With

    ''Populate the DataTable with sample data. You would be loading from SQL
    With MyDataSet.Tables(0)
        .Rows.Add(New String() {"Apple", "Red"})
        .Rows.Add(New String() {"Orange", "Orange"})
        .Rows.Add(New String() {"Banana", "Yellow"})
    End With

    ''These objects would normally be created automatically if you added DataTable to DataRepeater at design-time
    FruitLabel = New Label
    FruitTextBox = New TextBox
    ColorLabel = New Label
    ColorTextBox = New TextBox
    With FruitLabel
        .AutoSize = True
        .Location = New Point(10, 20)
        .Name = "FruitLabel"
        .Text = FRUIT_COL
    End With
    With ColorLabel
        .AutoSize = True
        .Location = New Point(10, 60)
        .Name = "FruitLabel"
        .Text = FRUIT_COL
    End With
    With FruitTextBox
        .Location = New Point(50, 20)
        .Size = New Size(60, 15)
        .DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyDataSet.Tables(0), FRUIT_COL, True))
    End With
    With ColorTextBox
        .Size = New Size(60, 15)
        .Location = New Point(50, 60)
        .DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyDataSet.Tables(0), COLOR_COL, True))
    End With

    ''Add the controls that will be displayed for each row in DataTable
    With DataRepeater1
        .ItemTemplate.Controls.Add(FruitLabel)
        .ItemTemplate.Controls.Add(FruitTextBox)
        .ItemTemplate.Controls.Add(ColorLabel)
        .ItemTemplate.Controls.Add(ColorTextBox)
    End With

    ''Run-time population of DataRepeater from your DataTable
    DataRepeater1.DataSource = MyDataSet
    DataRepeater1.DataMember = MyDataSet.Tables(0).TableName

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Example of how you can add additional rows with form elements
    With MyDataSet.Tables(0)
        .Rows.Add(New String() {"Grapes", "Green"})
    End With
End Sub
End Class

Now that you have seen the code, I hope you don't use it :)
I suggest you either set up your DataSet structure at design-time or use a different control to display your data.

Here's a link that has lots of information about the typical way to use DataRepeater: Link

Final Edit

The user's last bug was a case-sensitivity issue. The string "Text" in the following line of code must be capitalized to match the control's property name. I recommend creating a const for "Test" to avoid this typo.

[ControlName].DataBindings.Add(New System.Windows.Forms.Binding("Text", [DataTable], [Column Name], True))
bluecoder
looking for a code example. And in vb.net. I don't know anyting about aspx. I know what it would look like as I can do this in the designer but I want to do this in code.
Linda
Just to clarify, is this for a Winforms (desktop) or Web application? Do you just need to code to load data into a DataRepeater or are you looking for code that creates a DataRepeater at runtime and inserts it into the form?
bluecoder
Winform. Need code to connect the data to the repeater.
Linda
How much do you want to do at design-time and what do you need [o do at run-time? This will tell me what sample code to show you. Which of these do you want to do via the designer (the rest will be in the code] 1) Add DataRepeater to form 2) Design dataset/datatable [Column names, etc] 3) Load data to dataset 4) Others?
bluecoder
Datarepeater has been added in desgin.I have the ds/dt in code (my backend is sql server).what I need is how to get it to appear on the repeater. In the FormLoad I have this: Me.DataRepeater1.DataSource = myDSbut no data appears in the repeater. I remeber once seeing that I need to bind each data elements (only two in this case) but I don't remember how and where to do this.
Linda
Bluecoder,Here is my code which I tried to do like the one you show here (but my form controlls are set in design), but this code does not work. The repeater shows me each record, since I have have 3 records in this test data I get three lines on the form, but the content of the textbox (CustomerGroup) is blank, even thought I tested the myds and see that it is populated correctly.
Linda
Dim sql As String = "Select * from tblCustomerGroup Order by CustomerGroup"myDS = New DataSetUsing conn As New SqlConnection My.Settings.OrderEntrySalesConnectionString)onn.Open() Using ad As New SqlDataAdapter With ad .SelectCommand = New SqlCommand(sql, conn) .SelectCommand.CommandType = CommandType.Text .Fill(myDS, "CustomerGroup") End With End UsingEnd Using
Linda
Me.CustomerGroupTextBox.DataBindings.Add(New System.Windows.Forms.Binding("text", myDS.Tables(0), "CustomerGroup", True))Me.DataRepeater1.DataSource = myDSMe.DataRepeater1.DataMember = myDS.Tables(0).TableName
Linda
sorry, i guees I don't know how to post code here..
Linda
I will try to decipher your code and see if I can spot a problem. In the meantime, editt your question with your code rather than using comments. There are lots of formatting options and a preview window
bluecoder
1) "Text" in the ...Forms.Binding("Text"... line is case sensitive. You need to capitalize the "T" 2) Where are you adding your controls to the DataRepeater's ItemTemplate?
bluecoder
bluecoder - YOU NAILED IT - changing the text to Text and it works! I never would have suspected that this text is case sensitive. I'm going to check other code that I have and make sure that it's always Text. thanks!!
Linda
Happy to help. You might save yourself some frustration by declaring a const like CONST TEST_PROP as String = "Test" and then use TEST_PROP in your Binding statements. That way Visual Studio will catch your typos :)
bluecoder
A: 
Dim data As DataSet
DataRepeater1.DataSource = data
DataRepeater1.DataBind()
Adir
when I type in DataRepeater1.DataBind() it says:DataBind is not a member of Microsoft.VisualBasic.PowerPacks.DataRepeater
Linda
I would use other data control instead of the DataRepeater, look on the Data tab in the controls tab
Adir
I don't know why. But for me, I do know that I want to use the Datarepeater.
Linda