Link to home
Start Free TrialLog in
Avatar of DigitalDan3
DigitalDan3

asked on

Adding a column to a datatable and populating column with default value

I am using the following code to add a column to an existing datatable:

 Dim dcStatus As New DataColumn("Status", GetType(String))
            m_MissingDocsTable.Columns.Add(dcStatus)

            For i As Integer = 0 To m_MissingDocsTable.Rows.Count - 1
                Dim dr As DataRow = m_MissingDocsTable.Rows(i)
                dr("Status") = "Print"
            Next

I am then binding the DataTable to a datagrid.  The "Status" Column shows in the grid but the value of each row is null.  

What am I doing wrong?

Dan

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Try this with DataRow.EndEdit:

           For i As Integer = 0 To m_MissingDocsTable.Rows.Count - 1
                Dim dr As DataRow = m_MissingDocsTable.Rows(i)
                dr("Status") = "Print"
                dr.EndEdit()
            Next


Bob
Avatar of Sancler
Sancler

Try

            dcStatus.DefaultValue = "Print"

just before

            m_MissingDocsTable.Columns.Add(dcStatus)

Roger
Avatar of DigitalDan3

ASKER

Grid still returning Null for "Status" Column

   Dim dcStatus As New DataColumn("Status", GetType(String))
            m_MissingDocsTable.Columns.Add(dcStatus)

            For i As Integer = 0 To m_MissingDocsTable.Rows.Count - 1
                Dim dr As DataRow = m_MissingDocsTable.Rows(i)
                dr("Status") = "Print"
                dr.EndEdit()
            Next

            Me.gridMissingDocs.DataSource = MissingDocsTable
I added a message box to see what the Value is  and it does return "Print"

 Dim dcStatus As New DataColumn("Status", GetType(String))
            m_MissingDocsTable.Columns.Add(dcStatus)

            For i As Integer = 0 To m_MissingDocsTable.Rows.Count - 1
                Dim dr As DataRow = m_MissingDocsTable.Rows(i)
                dr("Status") = "Print"
                dr.EndEdit()
            Next
            MessageBox.Show(MissingDocsTable.Rows(0)("Status"))
            Me.gridMissingDocs.DataSource = MissingDocsTable
Do you have a DataGridTableStyle defined?  Are you mapping to the column?

Bob
The Setting of the Default Value worked but why does MessageBox show "Print" but the datagrid shows "Null"
I was going to add a TableStyle after I knew the information is correct.  
I added the tablestyle and it still return null
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forget the dv.Table line.  That was just me experimenting to see if a dataview made any difference.

Roger
just a question

you are adding a column to table  m_MissingDocsTable

but you use a different table for the grid ?

Me.gridMissingDocs.DataSource = MissingDocsTable