0

有一个 DataGridView 是在运行时创建的,从数据库中获取数据。数据网格视图中有四种类型的列:

  1. DataGridViewTextBoxColumn
  2. DataGridViewComboboxColumn
  3. DataGridViewButtonColumn
  4. DataGridViewCheckBoxColumn

创建该datagridview的代码如下:

    _form.DGV_.AutoGenerateColumns = False
    _form.DGV_.Columns.Clear()

    'Required Variables
    Dim pom_DataGridViewTextBoxColumn As DataGridViewTextBoxColumn = Nothing
    Dim pom_DataGridViewComboBoxColumn As DataGridViewComboBoxColumn = Nothing
    Dim pom_DataGridViewButtonColumn As DataGridViewButtonColumn = Nothing
    Dim pom_DataGridViewCheckBoxColumn As DataGridViewCheckBoxColumn = Nothing

    'Column-1 (ID: jntDate)
    pom_DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
    pom_DataGridViewTextBoxColumn.DataPropertyName = "jntDate"
    pom_DataGridViewTextBoxColumn.HeaderText = "Date"
    pom_DataGridViewTextBoxColumn.DefaultCellStyle.Format = "d"
    _form.DGV_.Columns.Add(pom_DataGridViewTextBoxColumn)

    'Column-2 (ID: jntAcc)
    pom_DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn
    pom_DataGridViewComboBoxColumn.DataPropertyName = "jntAcc"
    'Cond: If dataset Acc does not contain any table then add table from database.
    If Acc.Tables.Count > 0 Then
        pom_DataGridViewComboBoxColumn.DataSource = Acc.Tables.Item(0)
    Else
        Acc.Tables.Add(Tbl_Select("SELECT *, accNo AS byNo, accName as byName FROM tblAccounts", False, ""))
        pom_DataGridViewComboBoxColumn.DataSource = Acc.Tables.Item(0)
    End If
    'Cond: If _Account_Name_Number is True then set DisplayMember property to 'byName'.
    If _Account_Name_Number Then
        pom_DataGridViewComboBoxColumn.DisplayMember = "byName"
    Else
        pom_DataGridViewComboBoxColumn.DisplayMember = "byNo"
    End If
    pom_DataGridViewComboBoxColumn.ValueMember = "accID"
    pom_DataGridViewComboBoxColumn.HeaderText = "Account"
    _form.DGV_.Columns.Add(pom_DataGridViewComboBoxColumn)


    'Column-3 (ID: xxxx)
    pom_DataGridViewButtonColumn = New DataGridViewButtonColumn
    pom_DataGridViewButtonColumn.DataPropertyName = "xxxx"
    pom_DataGridViewButtonColumn.UseColumnTextForButtonValue = True
    pom_DataGridViewButtonColumn.HeaderText = "X"
    pom_DataGridViewButtonColumn.Text = "X"
    pom_DataGridViewButtonColumn.Width = 32
    pom_DataGridViewButtonColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    pom_DataGridViewButtonColumn.DefaultCellStyle.ApplyStyle(pom_DataGridViewButtonColumn.DefaultCellStyle)
    _form.DGV_.Columns.Add(pom_DataGridViewButtonColumn)

    'Column-4 (ID: jntReview)
    pom_DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn
    pom_DataGridViewCheckBoxColumn.DataPropertyName = "jntReview"
    pom_DataGridViewCheckBoxColumn.HeaderText = "Review"
    pom_DataGridViewCheckBoxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    pom_DataGridViewCheckBoxColumn.DefaultCellStyle.ApplyStyle(pom_DataGridViewCheckBoxColumn.DefaultCellStyle)
    _form.DGV_.Columns.Add(pom_DataGridViewCheckBoxColumn)

在 WPF 应用程序中,需要用这四列填充相同的 DataGrid。如何在 WPF 的 DataGrid 中创建数据并将数据绑定到四种类型的列?

4

1 回答 1

1

为什么不跳过在 cs 代码中做所有这些事情呢?当数据网格需要更复杂的模板、数据触发器、自定义样式、动画等时,您将开始把自己固定在角落里,并且会碰壁……此外,它不仅会让下一个人更容易,而且你也是。当您必须在 3-6 个月后在 .cs 代码中创建的野兽中添加/修复某些内容时,您可能需要很长时间才能理解发生了什么:)

相反,在 xaml 中完成所有工作,创建一个 UserControl,将此数据网格放入其中,将 DataContext 从 UserControl 的各种实例传递到它的 DataGrid。

于 2013-06-12T18:15:07.793 回答