[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2005-09-27
11
Medium Priority
?
3,335 Views
Last Modified: 2011-10-03
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

0
Comment
Question by:DigitalDan3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14968458
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
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14968681
Try

            dcStatus.DefaultValue = "Print"

just before

            m_MissingDocsTable.Columns.Add(dcStatus)

Roger
0
 

Author Comment

by:DigitalDan3
ID: 14968692
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:DigitalDan3
ID: 14968758
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14968851
Do you have a DataGridTableStyle defined?  Are you mapping to the column?

Bob
0
 

Author Comment

by:DigitalDan3
ID: 14968877
The Setting of the Default Value worked but why does MessageBox show "Print" but the datagrid shows "Null"
0
 

Author Comment

by:DigitalDan3
ID: 14968917
I was going to add a TableStyle after I knew the information is correct.  
0
 

Author Comment

by:DigitalDan3
ID: 14969091
I added the tablestyle and it still return null
0
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 14969360
I don't see why it should have the effect you're reporting, or indeed any effect at all, but the code that's working for me is slightly different from yours

Try this

            Dim dc2 As New DataColumn
            With dc2
                .ColumnName = "Status"
                .DataType = System.Type.GetType("System.String")
                .DefaultValue = "Print"
            End With

            ds.Tables(0).Columns.Add(dc2)

            dv.Table = ds.Tables(0)
            dg1.DataSource = ds.Tables(0)

See if there's something about the precise way in which it is constructed.  My datagrid - no tablestyle - is showing "Print".

Roger
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14969376
Forget the dv.Table line.  That was just me experimenting to see if a dataview made any difference.

Roger
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 14969604
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question