0

There is a DataGridView which is created at runtime with data fetching from database. There are four types of columns in data grid view:

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

Code for creating that datagridview is as below:

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

In WPF application, there is a need to populate same DataGrid with these four columns. How can I create and bind data to four types of columns in DataGrid in WPF ?

1 Answer 1

1

Why not skip doing all that stuff in cs code? You'll start pinning yourself in the corner and WILL hit the wall when the datagrid will need more complex templates, data triggers, custom styles, animations, etc... Besides, it will make it easier not only on the next person, but you as well. When you have to add/fix something in the beast you create in .cs code 3-6 months down the road, it might take you quite a while to understand what is going on :)

Instead, do all your work in xaml, Create a UserControl, put this datagrid in it, Pass the DataContext from various instances of the UserControl down to it's DataGrid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.