none
how to add a row to a datagridview bound to a bindingsource RRS feed

  • Question

  • I have a datagridview bound to a bindingsource and I want to add an  empty row to the datagridview allowing the user to input the data in the different columns. While updating to the Database, it seems there is an extran empty row and it contradicts the nonnull constraint.  Thanks. Here is the code:

    /////////////////////////////////////////////////////////////////////////////

    In the form load

    ////////////////////////////////////////////////////////////////////////////

            obj_CmdFournisseur.Connection = G_ObjconnectionFixe

            obj_DataFournisseur = New SqlDataAdapter(obj_CmdFournisseur)

            ' Create a command builder object

            Obj_Builder = New SqlCommandBuilder(obj_DataFournisseur)

            Obj_Builder.QuotePrefix() = "["

            Obj_Builder.QuoteSuffix = "]"

            obj_DSFournisseur.Clear()

            obj_DataFournisseur.Fill(obj_DSFournisseur, "fournisseurs")

       

            With ComboBox1.Items

                .Add("Tous")

                .Add("Actifs seuls")

                .Add("Effaces seuls")

                .Add("Solde positif")

                .Add("Solde negatif")

            End With

            IsFirstUseofAllOption = True

            ComboBox1.SelectedIndex = 0

     

    ///////////////////////////////////////////////////////////////////

    In the combobox1 selectedindexchanged

    ///////////////////////////////////////////////////////////////

            With ComboBox1

                Select Case .SelectedIndex

                    Case Is = 0     ' option tous

                        DataViewFournisseurs = New    ataView(obj_DSFournisseur.Tables("fournisseurs"))

                        DtTable = DataViewFournisseurs.ToTable()

                        Str_Filtre = ""

                        Bl_ButtonState = True

                        bsSource.SuspendBinding()

                        bsSource.DataSource = DataViewFournisseurs

                        bsSource.Filter = Str_Filtre

                        bsSource.Sort = "[nom fournisseur] ASC"

                        bsSource.ResumeBinding()

                        With DataGridView1

                           .DataSource = bsSource

                        End With

     

     

    ///////////////////////////////////////////////////////////////////////////

    In the add button

    ///////////////////////////////////////////////////////////////////////////

              Dim Obj_NewRow As DataRow

            Obj_NewRow = obj_DSFournisseur.Tables("fournisseurs").NewRow

            Obj_NewRow.Item("nom fournisseur") = ""

            Obj_NewRow.Item("adresse") = ""

            Obj_NewRow.Item("ville") = ""

            Obj_NewRow.Item("telephone") = ""

            Obj_NewRow.Item("solde") = 0.0

            Obj_NewRow.Item("efface") = False

            obj_DSFournisseur.Tables("fournisseurs").Rows.Add(Obj_NewRow)

    Friday, June 12, 2020 10:10 PM

Answers

  • Hi Karen;

    Once I use the bindingsource addnew property, how do i get the datagridview focus on the newly added row so as to allow the user to fill the columns? Thank you.

    AddNew is not a property, AddNew is a method. Once executing AddNew the current row is now the new row. 

    BindingSource.AddNew Method


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by vblover29 Sunday, June 14, 2020 6:33 PM
    Saturday, June 13, 2020 9:40 PM
    Moderator

All replies

  • Hello,

    Don't directly add a new DataRow, use BindingSource.AddNew then in DefaultValuesNeeded you can seed any value you want and see also CellValidating event. I'd sugged subscribing to DataError event when tinkering with this. When the user leaves the new row that is valid it' got a negative primary key value until a save/update is done. If there are constraint errors when leaving the row you need to handle this.

    Side note: Personally when I was using DataGridView/DataTable/BindingSource my choice was to pull up a model dialog to add and edit and if the user wanted to add multiple new rows they could as I would have a delegate to push new rows back to the calling form which then would immediately display in the DataGridView. Much more control then doing this in a DataGridView.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, June 13, 2020 2:00 AM
    Moderator
  • While updating to the Database, it seems there is an extran empty row and it contradicts the nonnull constraint.  Thanks. Here is the code:

            Dim Obj_NewRow As DataRow

       

    You should not add a DataRow but an obj_DSFournisseur.Tables("fournisseurs").NewRow

    https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.newrow?view=netcore-3.1


    Success
    Cor

    Saturday, June 13, 2020 12:27 PM
  • Hi Karen;

    Once I use the bindingsource addnew property, how do i get the datagridview focus on the newly added row so as to allow the user to fill the columns? Thank you.

    Saturday, June 13, 2020 8:43 PM
  • Hi cor;

    I checked the exemple given the  link you suggested. I find no difference in the steps  in the exemple and my own code. Thanks.

    Saturday, June 13, 2020 8:46 PM
  • Hi Karen;

    Once I use the bindingsource addnew property, how do i get the datagridview focus on the newly added row so as to allow the user to fill the columns? Thank you.

    AddNew is not a property, AddNew is a method. Once executing AddNew the current row is now the new row. 

    BindingSource.AddNew Method


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by vblover29 Sunday, June 14, 2020 6:33 PM
    Saturday, June 13, 2020 9:40 PM
    Moderator