我是www.example.com的新手,正在学习使用datagrid。我已经成功地将它绑定到一个可观察的集合,但想将它绑定到一个数据表。我已经看了SO上的许多答案,但不能理解其中的大多数。在阅读了其他人问题的答案后,我尝试了许多不同的方法,但在尝试了4个晚上后,似乎仍然不能使它工作!VB.net/MVVM/WPF/xaml and I'm learning to use the datagrid. I have successfully bound it to an observable collection but want to bind it to a datatable. I have looked at a number of answers on SO but cannot understand most of them. I have attempted to try a number of various ways after reading answers to other peoples questions but still can't seem to make it work after 4 evenings trying! I have got to the point of being able to add rows (no data in them) but no columns or data in columns. Another post suggested creating the table then binding it, another setting the datatable to null (created an error) prior, another who had the same issue but no answer was presented and another that said columns needed headers first. I'm a bit confused.
其他控件绑定正常,所以我假设不是这样,我可能犯了一个相当愚蠢的错误。我想保持它使用绑定,而不是直接引用数据网格,我在其他答案中看到过。此外,我想避免Linq的时刻,因为只是学习WPF是一个有点陡峭的曲线。
当前代码;
主窗口. xaml. vb,公共子窗口新建;
Me.DataContext = myModelView
装订;
<DataGrid ItemsSource="{Binding dtCharDG, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True" Margin="0,152,0,251" />
结合性(dt应为dv);
Public Property dtCharDG() As DataView
Get
Return dtCharDGValue
End Get
Set(dtCharDGValue As DataView)
NotifyPropertyChanged()
End Set
End Property
呼叫,此刻只是倾倒在一个按钮上;
Dim dtCharDGTemp As New DataTable
CreateCharGrid("Hello World", dtCharDGTemp)
dtCharDG = dtCharDGTemp.DefaultView
Sub;
Public Sub CreateCharGrid(ByVal CharSet As String, ByRef dtToFill As DataTable)
'Dim As New DataTable
Dim x As Integer
Dim y As Integer
Dim workColumn As New DataColumn
Dim workRow As DataRow
x = Len(CharSet)
For y = 0 To x - 1
workColumn = New DataColumn()
dtToFill.Columns.Add(workColumn)
Next
workRow = dtToFill.NewRow()
For y = 0 To x - 1
workRow(y) = y + 1
Next
dtToFill.Rows.Add(workRow)
End Sub
我已经把它搞得一团糟了,可能有很多东西需要修复,我只是迷路了,我的目的是用字符串中的字符填充一个数据网格,在第二行和第一行中,每列一个整数,显示它在字符串中的位置。
从相对的新手那里得到的任何帮助。
1条答案
按热度按时间dldeef671#
看起来您只是缺少DataTable.Rows.Add()
更多信息请参见此处... https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/adding-data-to-a-datatable
如果您希望字符串中的列是数字,第一行是字母,请尝试以下更改