在运行时在WPF中创建DataGrid

时间:2013-06-12 17:55:34

标签: wpf vb.net visual-studio-2010 datagrid

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

  1. DataGridViewTextBoxColumn
  2. DataGridViewComboboxColumn
  3. DataGridViewButtonColumn
  4. DataGridViewCheckBoxColumn
  5. 创建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中创建数据并将数据绑定到四种类型的列?

1 个答案:

答案 0 :(得分:1)

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

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