Results 1 to 4 of 4

Thread: [3.0/LINQ] Data binding typed dataset textboxes, datagridview

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    [3.0/LINQ] Data binding typed dataset textboxes, datagridview

    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();
                }
            }
    Attached Images Attached Images  
    steve

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [3.0/LINQ] Data binding typed dataset textboxes, datagridview

    What is wrong with this method? It seems to do what you want by clearing out the boxes and readies the dataset for a new record.

    Quote Originally Posted by steve_rm

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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [3.0/LINQ] Data binding typed dataset textboxes, datagridview

    Hello,

    The client wanted something more automated. However, since you are the only one who has replied, I guess there is no other way of doing this.

    However, I will keep this post up for another 24hrs incase. Then I will mark this thread resolved.

    Thanks,
    steve

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [3.0/LINQ] Data binding typed dataset textboxes, datagridview

    Quote Originally Posted by steve_rm
    Hello,

    The client wanted something more automated. However, since you are the only one who has replied, I guess there is no other way of doing this.

    However, I will keep this post up for another 24hrs incase. Then I will mark this thread resolved.

    Thanks,
    Why not just allow them to enter data into the data grid?

    It can't get any easier than that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width