英文:
Binding datagrid to datatable
问题
我是你的中文翻译,以下是代码部分的翻译:
I'm fairly new to 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.
Other controls bind ok so I assume it's not that and I'm probably making a fairly stupid error. I would like to keep it using bindings and not referencing the datagrid directly which I have seen in other answers. Also, I would like to avoid Linq at the moment as just learning WPF is a bit of steep curve.
Current code;
Mainwindow.xaml.vb, Public Sub New;
~~~
Me.DataContext = myModelView
~~~
Binding;
~~~
<DataGrid ItemsSource="{Binding dtCharDG, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True" Margin="0,152,0,251" />
~~~
Binding Property (dt should be dv);
~~~
Public Property dtCharDG() As DataView
Get
Return dtCharDGValue
End Get
Set(dtCharDGValue As DataView)
NotifyPropertyChanged()
End Set
End Property
~~~
Call, just dumped in a button at the moment;
~~~
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
~~~
I've messed around with it so much there's probably quite a lot to fix and I've just got lost. The intention is to fill a datagrid with the characters from string in separate columns in the second row and in the top row an integer per column showing where it is in the string.
Any help gratefully received from a relative newbie.
如果你有任何需要更改的地方或其他问题,请随时提出。
英文:
I'm fairly new to 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.
Other controls bind ok so I assume it's not that and I'm probably making a fairly stupid error. I would like to keep it using bindings and not referencing the datagrid directly which I have seen in other answers. Also, I would like to avoid Linq at the moment as just learning WPF is a bit of steep curve.
Current code;
Mainwindow.xaml.vb, Public Sub New;
Me.DataContext = myModelView
Binding;
<DataGrid ItemsSource="{Binding dtCharDG, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True" Margin="0,152,0,251" />
Binding Property (dt should be dv);
Public Property dtCharDG() As DataView
Get
Return dtCharDGValue
End Get
Set(dtCharDGValue As DataView)
NotifyPropertyChanged()
End Set
End Property
Call, just dumped in a button at the moment;
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
I've messed around with it so much there's probably quite a lot to fix and I've just got lost. The intention is to fill a datagrid with the characters from string in separate columns in the second row and in the top row an integer per column showing where it is in the string.
Any help gratefully received from a relative newbie.
答案1
得分: 1
It looks like you are just missing DataTable.Rows.Add()
See here for more info...
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/adding-data-to-a-datatable
If you are wanting columns to be numbers and 1st row to be letters in the string try this change
For y = 0 To x - 1
workColumn = New DataColumn(y.ToString())
dtToFill.Columns.Add(workColumn)
Next
workRow = dtToFill.NewRow()
For y = 0 To x - 1
workRow(y) = CharSet.Substring(y, 1)
Next
dtToFill.Rows.Add(workRow)
英文:
It looks like you are just missing DataTable.Rows.Add()
See here for more info...
<https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/adding-data-to-a-datatable>
If you are wanting columns to be numbers and 1st row to be letters in the string try this change
For y = 0 To x - 1
workColumn = New DataColumn(y.ToString())
dtToFill.Columns.Add(workColumn)
Next
workRow = dtToFill.NewRow()
For y = 0 To x - 1
workRow(y) = CharSet.Substring(y, 1)
Next
dtToFill.Rows.Add(workRow)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论