Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Data binding business object to bindingsource
 

Data binding business object to bindingsource

Hi, i have a little problem. I have done an application that use a command patternwhich comunicate to business logic layer (BLL). In my windows form i have set 6 textbox (Year, Date, Hour, Serial, Id, idtransport) and a datagridview (Master-Detail), with two bindingsource, one for textboxs (master), and the second bindingsource bind to datagridview (detail). In detailBindingSource_addingNew event i execute a create object with command pattern, and in detailBindingSource_ListChanged, i update the current row into database (using command pattern that it comunicate to BLL, and BLL comunicate to DAL). My detailTable in SQL Server have a IDENTITY column that i get an autonumeric id.

But i have aproblem when i click in add button for to create a new Master register. If i use a masterBindingSource_addingNew eventfor to call a command pattern and to create register in database, i need before to inform the serial. But if call a masterbindingSource.Addnew(), it reset all textbox and execute masterBindingSource_addingNewwhichcall a command patternwhich create business object and execute a SQL query that obtaing a id, because the masterTable have a IDENTITY column, but it show a error because the serial textbox was in blank.

In other words, i need to can inform the textbox and then to send information to database, using a command pattern and a IDENTITY column, that depend of the other column.

Primary key (Year,Serial, Id)


Sorry for my english.


Albert Gassó
Computer Software Engineer
Barcelona (Spain)
  • Moved byHarry ZhuMSFTTuesday, September 29, 2009 4:15 AMrelating to databinding (From:Visual C# Language)
  •  
Albert Gassó  Friday, September 25, 2009 12:02 PM
The only thing I feel certain about is that your events are not firing in the order you coded them to. That's far too much to swallow without any code. I recommend you write a simplified short compilable example for us to work with.
おろ?
P.Brian.Mackey  Friday, September 25, 2009 7:47 PM
Sorry. I put here my code.

        private void recepciobindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            /* This event will be called several times during form initialization.
             * We don't want to do anything with it until the runtime authors
             * list has been passed in. */

            // Exit if no project list
            
            if (m_Recepcions == null) return;

            // Create a new project
            CommandCreateRecepcio createRecepcio = new CommandCreateRecepcio();
            Recepcio newRecepcio = (Recepcio)m_AppController.ExecuteCommand(createRecepcio);

            /* Since the BindingSource is managing the collection, we pass the new 
             * recepcio to the BindingSource, using the NewObject property of the 
             * event args. The BindingSource will add the new author to the 
             * RecepcioList for us. */

            // Add it to theAuthors list
            e.NewObject = newRecepcio;
        }
        private void FormRecepcio_Load(object sender, EventArgs e)
        {
            // Initialize controller
            m_AppController = new AppController();

            /* Note that the DataProvider class is static, so it doesn't
             * get instantiated. */

            // Get authors list
            CommandGetRecepcions getRecepcions = new CommandGetRecepcions();
            m_Recepcions = (RecepcioList)m_AppController.ExecuteCommand(getRecepcions);

            // Bind grids
            recepciobindingSource.DataSource = m_Recepcions;
            liniaRecepcioListBindingSource.DataSource = recepciobindingSource;
            liniaRecepcioListBindingSource.DataMember = "LiniesRecepcio";

            // Enllaça els camps de text al conector de dades
            this.txtExercici.DataBindings.Add("Text", recepciobindingSource, CAMP_CAPCALERA_EXERCICI);
            this.txtSerie.DataBindings.Add("Text", recepciobindingSource, CAMP_CAPCALERA_SERIE);
            this.txtNumero.DataBindings.Add("Text", recepciobindingSource, CAMP_CAPCALERA_IDRECEPCIO);
            this.txtData.DataBindings.Add("Text", recepciobindingSource, CAMP_CAPCALERA_DATA, true, DataSourceUpdateMode.OnValidation, "", "d");
            this.txtHora.DataBindings.Add("Text", recepciobindingSource, CAMP_CAPCALERA_HORA, true, DataSourceUpdateMode.OnValidation, "", "t");
            this.txtTransportista.DataBindings.Add("Text", recepciobindingSource, CAMP_CAPCALERA_TRANSPORTISTA);

            // Enllaça els controls del DataGridView als components del
            // BindingSource i carrega les dades desde la base de dades.
            customDataGridView1.DataSource = liniaRecepcioListBindingSource;

        }
        private void btNou_Click(object sender, EventArgs e)
        {
            recepciobindingSource.AddNew();
        }



Albert Gassó
Computer Software Engineer
Albert Gassó  Friday, September 25, 2009 9:30 PM
I have deleted the AddingNew event, and in the btAccept_Click button event, I have set the command pattern calls. And I have modified the New Button event.

        private void btNou_Click(object sender, EventArgs e)
        {
            //DesbloquejarCamps();
            recepciobindingSource.EndEdit();

            this.btEliminar.Enabled = false;
            this.btModificar.Enabled = false;
            this.btNou.Enabled = false;

            this.btAcceptar.Enabled = true;
            this.btCancelar.Enabled = true;

            txtData.Focus();

            txtExercici.Text = String.Empty;
            txtSerie.Text = String.Empty;
            txtNumero.Text = String.Empty;
            txtData.Text = String.Empty;
            txtTransportista.Text = String.Empty;

            this.InicialitzaCamps();
        }

        private void btAcceptar_Click(object sender, EventArgs e)
        {
            Validate();
            recepciobindingSource.EndEdit();
            // Create a new project
            CommandCreateRecepcio createRecepcio = new CommandCreateRecepcio(int.Parse(txtExercici.Text), 
                txtSerie.Text, int.Parse(txtNumero.Text), txtData.Text, txtTransportista.Text);
            Recepcio newRecepcio = (Recepcio)m_AppController.ExecuteCommand(createRecepcio);
        }
But, i don't know if this is right. Besides that no refresh the bindingSource.


Albert Gassó
Computer Software Engineer
Albert Gassó  Tuesday, September 29, 2009 7:50 AM

Hi,

 

You can set the PrimaryKey property to your datatable. It will check identity automatically.

DataTable.PrimaryKey Property

http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey.aspx

 

            DataTable dtMultiKey = new DataTable();

            dtMultiKey.Columns.Add("Key1");

            dtMultiKey.Columns.Add("Key2");

            dtMultiKey.Columns.Add("Key3");

            dtMultiKey.Columns.Add("Tag");

 

            dtMultiKey.PrimaryKey = new DataColumn[3] { dtMultiKey.Columns[0], dtMultiKey.Columns[1], dtMultiKey.Columns[2] };

 

            dtMultiKey.Rows.Add("1", "aa", "bb");

 

            dataGridView1.DataSource = dtMultiKey;

 

Best regards,

Ling Wang


Please remember to click “Mark as Answer?on the post that helps you, and to click “Unmark as Answer?if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, October 02, 2009 3:19 PM

You can use google to search for other answers

Custom Search

More Threads

• Winforms Tabcontrol issue
• selected value of DataGrid
• Multi-line (merged) datagridview header
• Putting Info into a sql database
• Adding a property to underlying bound object does not add corresponding column to bound datagridview
• Set DatagridviewComboBoxColumn Default Value
• Is ListBox the only control in Forms to allow multiple selections?
• custom scrolling in a datagrid...
• Problem in Typed Dataset of VS 2005
• How do I get the selected item from a datagrid