vb.net. How do I bind dataset to DataRepeater?

心已入冬 提交于 2019-12-22 10:15:27

问题


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:


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))



回答2:


Dim data As DataSet
DataRepeater1.DataSource = data
DataRepeater1.DataBind()


来源:https://stackoverflow.com/questions/2226886/vb-net-how-do-i-bind-dataset-to-datarepeater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!