none
Typed Datatable.NewRow() does not display default values in DataBound controls RRS feed

  • Question

  • I have a typed datatable (DtOrder), manually created for data entry.  Each column of DtOrder has a default value.  

    I created a new BindingSource (BsOrder) with its DataSource set to DtOrder.

    My data entry window has controls which are Bound (via BsOrder) to the columns of DtOrder.  Most of this is done in the FormLoad event as follows;

    CreateOrderTable()       
    BsOrder.DataSource = DtOrder
    SetBindings()
    NewOrderRow = DtOrder.NewRow()

    I add a NewRow to DtOrder as the last step and expect to see the default values appear in the bound textboxes.  However this does not occur and the textboxes remain blank.

    Can anyone tell me how the default values can be loaded automatically after the statement.

    NewOrderRow = DtOrder.NewRow()

    ps. I tried using a BindingNavigator to generate the NewRow and found that it loads the defaults.  However, I would like to avoid using it if possible.

    Thanks,

    Rinso

    Wednesday, May 23, 2018 8:37 AM

All replies

  • If it is a Typed DataSet, then don't use NewRow, but the name given to that. Something like NewWhateverRow. It is mostly the first time very confusing because it are generated names. 


    Success
    Cor



    Wednesday, May 23, 2018 9:12 AM
  • Hi Rinso,

    When you create datatable, you need to set default value for datatable column, like this:

      dt.Columns.Add("Id", GetType(Int32)).DefaultValue = 0
            dt.Columns.Add("FirstName", GetType(String)).DefaultValue = "Please enter"
            dt.Columns.Add("lastName", GetType(String)).DefaultValue = "please enter"
    
            dt.Rows.Add(1, "Cherry", "Bu")
            dt.Rows.Add(2, "mattew", "wu")
            dt.Rows.Add(3, "barry", "wang")
            dt.Rows.Add(4, "wendy", "zang")
            dt.Rows.Add(5, "Nick", "qian")
    
       Dim newrow As DataRow = dt.NewRow()
            Dim id = newrow("Id")
            Dim firstname = newrow("FirstName")
            Dim lastname = newrow("lastName")

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 24, 2018 2:44 AM
    Moderator
  • Ok.  Not really understanding what you are saying.  Perhaps more details from me may assist.  Here are samples of my Variable declarations, FormLoad Sub, CreateDatatable Sub, SetBindings Sub

    Option Explicit On
    Option Strict On
    
    Imports System.Data.OleDb
    
    Public Class FormOrderReceiving
    
    #Region "Variables"
    
        '--- Binding variables
        Friend WithEvents BsOrder As New BindingSource
        Friend WithEvents DtOrder As DataTable
        Friend WithEvents BsBrands As New BindingSource
        Friend WithEvents DaBrands As OleDb.OleDbDataAdapter
        Friend WithEvents BsSup As New BindingSource
        Friend WithEvents DaSup As OleDb.OleDbDataAdapter
        Friend WithEvents BnOrder As New BindingNavigator(True)
        Private NewOrderRow As DataRow
    Private Sub FormOrderReceiving_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Try
                Me.Text = "Order Receiving"
                CreateOrderTable()
                BsBrands = GetbsBrands()
                BsSup = GetbsSupplier()
                BsOrder.DataSource = DtOrder
                SetBindings()
                NewOrderRow = DtOrder.NewRow()
                BsOrder.ResetBindings(False)
                Me.Invalidate()
                DGVMain.DataSource = BsOrder
                BIsinitialising = False
            Catch ex As Exception
                MessageBox.Show("Error : " & ex.Message, "FormOrderReceiving_Load", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub


    Private Sub CreateOrderTable()
    
            Dim column As DataColumn
    
            Try
                DtOrder = New DataTable
                '--- create datatable structure
                column = New DataColumn With {
                    .DataType = System.Type.GetType("System.String"),
                    .DefaultValue = "",
                    .ColumnName = "BrandName",
                    .Unique = False
                }
                DtOrder.Columns.Add(column)
    
                column = New DataColumn With {
                    .DataType = System.Type.GetType("System.String"),
                    .DefaultValue = "",
                    .ColumnName = "ItemCode",
                    .Unique = False
                }
                DtOrder.Columns.Add(column)
    
                column = New DataColumn With {
                    .DataType = System.Type.GetType("System.Int32"),
                    .DefaultValue = 1,
                    .ColumnName = "OrderQty",
                    .Unique = False
                }
                DtOrder.Columns.Add(column)
    Private Sub SetBindings()
    
            Try
                With CboSup
                    .DisplayMember = "SupName"
                    .ValueMember = "PK_SupId"
                    .DataSource = BsSup
                End With
    
                With CboBrand
                    .DisplayMember = "BrandName"
                    .ValueMember = "BrandName"
                    .DataSource = BsBrands
                End With
    
                Dim bc As Binding = New Binding("Text", BsOrder, "BrandName")
                CboBrand.DataBindings.Add(bc)
    
                TxtItemCode.DataBindings.Add("text", BsOrder, "ItemCode")
                TxtItemOrderQty.DataBindings.Add("text", BsOrder, "OrderQty")
                TxtItemColour.DataBindings.Add("text", BsOrder, "ItemColour")
                TxtItemRecdQty.DataBindings.Add("text", BsOrder, "RecdQty")
                TxtItemBuyPrice.DataBindings.Add("text", BsOrder, "BuyPrice")
                TxtItemDisc.DataBindings.Add("text", BsOrder, "Discount")
                TxtCurrentSellPrice.DataBindings.Add("text", BsOrder, "CurrentPrice")
                TxtNewSellPrice.DataBindings.Add("text", BsOrder, "NewPrice")
            Catch ex As Exception
                MessageBox.Show("Error : " & ex.Message, "SetBindings", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub

    I'm expecting that once the form is loaded that the "Default" values would be loaded in the bound Textboxes.  I've tried re-ordering the execution sequence in the FormLoad event to see if that fixes it but it does not.

    I suspect that it may have something to do with the NewRow being in a Detached state prior to my adding it to DtOrder after data entry is finalised.

    Any help appreciated.

    Cheers,

    Rinso

    Thursday, May 24, 2018 5:50 AM
  • What you show is not a typed datatable. It is a weak typed table. 

    In a typed datatable every property and method gets its own name. You cannot do it yourself by hand. You need generators for that. 

     

    However, that does not mean that you cannot use your current code. I think the problem is not in that, but in the part where you retrieve the table from the database (for instance is again a new datatable instanced)

    Do you know how to debug? 

     I assume you don't know how that goes. 

    Click on the line in the editor before the code (it has to be an expression). 

    Then click on Start or F5 

    The program stops on the point it reaches the dot. Then you can step through your code using the F10 or F11 


    Success
    Cor

    Thursday, May 24, 2018 11:34 AM
  • Friday, May 25, 2018 9:34 AM
  • Have you considered using AddingNew event of the BindingSource?

    Private Sub BsOrder_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) _
        Handles BsOrder.AddingNew
    
        Dim drv As DataRowView = DirectCast(BsOrder.List, DataView).AddNew()
        drv.Row.Item("Some field") = "Default value goes here"
        drv.Row.Item("Another frield") = "Default value goes here"
        e.NewObject = drv
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, May 25, 2018 11:44 AM
    Moderator
  • Thanks for your suggestions.  I've gone down a different path and have created an Orders Class combined with a List(of Orders) instead of using the DataTable structure.

    Please mark this question as closed.

    Friday, May 25, 2018 10:12 PM
  • Thanks for your suggestions.  I've gone down a different path and have created an Orders Class combined with a List(of Orders) instead of using the DataTable structure.

    Please mark this question as closed.


    The question is yours and you should mark those that assisted you or answered your question. There is no way to set the thread as closed other than to lock it.

    La vida loca

    Friday, May 25, 2018 11:59 PM