Search code examples
vb.netdatatabledatagridviewexpressiondatacolumn

why is the datatable expression column become ready only in vb.net


I'm trying to so I want to use the datatable expression column to print labels so below I made a simple example of moving with a checkbox from datagridview1 to datagridview2

I have the code below, but this is still wrong.

please guide me

Thanks

Public Class Form1
    Public dt As DataTable
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 dt = New DataTable
        dt.Columns.Add("A", GetType(String))
        dt.Columns.Add("B", GetType(String))
dt.Columns.Add("C", GetType(String), "TRIM(IIF(A Is Null Or SUBSTRING(A, 1, 1) = '-', '', A) + IIF(B Is Null Or SUBSTRING(B, 1, 1) = '-', '', ' ' + B))")
        dt.Rows.Add("BALOTELLY", "OLD WARDAH (02)")
        dt.Rows.Add("TOYOBO FODU THOMPSON", "LIGHT BLACK (12)")
        dt.Rows.Add("MCR", "-")
        dt.Rows.Add("WALLY COTTON CREAP", "-")
        dt.Rows.Add("WALLY COTTON CREAP", "BLACK (23)")
        dt.Rows.Add("-", "-")
        DataGridView1.DataSource = dt
        Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
        CheckedBoxColumn.Width = 40
        CheckedBoxColumn.Name = "checkboxcolumn"
        CheckedBoxColumn.HeaderText = "Check"
        DataGridView1.Columns.Insert(0, CheckedBoxColumn)
End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim count As Integer = 0
        For Each row2 As DataGridViewRow In DataGridView1.Rows
            Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
            If isselect Then
                count += 1
            End If
        Next row2
        Dim selectedRows As DataTable = dt.Clone
        For Each row2 As DataGridViewRow In DataGridView1.Rows
            Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
            If isselect Then
                Dim newRow As DataRow = selectedRows.Rows.Add()
                newRow("A") = dt.Rows(row2.Index)("A")
                newRow("B") = dt.Rows(row2.Index)("B")
'error below line code "Column "C" is ready only
                newRow("C") = dt.Rows(row2.Index)("C")
            End If
        Next row2
        DataGridView2.DataSource = selectedRows
        'theLabel.DataSource = selectedRows
    End Sub
End Class

enter image description here


Solution

  • according to the perfect answer from @dr.null

    I thank you for the help

          Dim selectedRows As DataTable = dt.Clone
    selectedRows.Columns("C").Expression = Nothing : selectedRows.Columns("C").ReadOnly = False