Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data binding text boxes and datagridview

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
0
0
TH
Hello,

VS 2008.

I am using a typed dataset and reading and writing to a xml file. The table is very small, so don't need any database.

I have sent an image of my window. This is not a master/child detail. The grid displays all the people, and the text boxes will display what is selected in the grid so that they can be edited and added.

I am having a small issue with adding a new person. I have found a work around, which means suspending binding on the bindingsource before clearing the contents of the text boxes. By clicking the clear button. The user will add a new person and then click add. I am thinking is there a way to do this without having the user click the clear button, to suspend binding.

I have also tried: which clears all the text boxes automatically.
DataRowView drv = (DataRowView)this.bsAddressBook.AddNew();

All the data binding has been set in the designer for the text boxes, datagrid and binding source.

This is not a big issue. I think I am just looking for a way just to have a 'Add New' and 'Save' button. The save will save any new person added, and save any updates. The add new will clear the text boxes so that the user can enter the details.

Code below:
Code:
private void AddressBook_Load(object sender, EventArgs e)
        {
            //Read in and fill the datagrid.
            this.ReadAddressBookXML();
        }

        //Add a new address book details to the xml file
        private void btnAddNew_Click(object sender, EventArgs e)
        {                  
            //Create a new data row
            dsCATDialer.AddressBookRow nr = dsCATDialer.AddressBook.NewAddressBookRow();

            nr.Name = this.txtName.Text;
            nr.Email = this.txtEmail.Text;
            nr.PhoneNumber = this.txtPhoneNumber.Text;
            nr.Notes = this.txtNotes.Text;
            
            //Add data row to the addressbook table
            dsCATDialer.AddressBook.Rows.Add(nr);

            //Resume binding so that the text boxes will contain the bounded details.
            if (this.bsAddressBook.IsBindingSuspended)
            {
                this.bsAddressBook.ResumeBinding();
            }

            //Move to the last record which is the most recent one added.
            this.bsAddressBook.MoveLast();

            //Save the changes to the xml file.
            this.WriteAddressBookXML();
        }

        //Save the currently selected row
        private void btnSave_Click(object sender, EventArgs e)
        {
            //Get the current row that is selected.
            DataRowView drv = (DataRowView)this.bsAddressBook.Current;

            //Enter the updated details.
            drv["Name"] = this.txtName.Text;
            drv["PhoneNumber"] = this.txtPhoneNumber.Text;
            drv["Email"] = this.txtEmail.Text;
            drv["Notes"] = this.txtNotes.Text;

            //Commit the changes to the dataset.
            this.bsAddressBook.EndEdit();

            //Resume binding so that the text boxes will display the bounded records.
            if (this.bsAddressBook.IsBindingSuspended)
            {
                this.bsAddressBook.ResumeBinding();
            }

            //Save the changes to the xml file.
            this.WriteAddressBookXML();
        }

        //Clear the contents before adding a new row
        private void btnClear_Click(object sender, EventArgs e)
        {
            //Suspend the binding so that text boxes can have there contents cleared.
            this.bsAddressBook.SuspendBinding();

            //Clear all the contents for filling in the new details.
            this.txtName.Clear();
            this.txtPhoneNumber.Clear();
            this.txtEmail.Clear();
            this.txtNotes.Clear();          
        }

        //Delete the currently selected record      
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete " + this.txtName.Text + "?", "Delete Item", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                this.bsAddressBook.RemoveCurrent();
            }
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top