Results 1 to 5 of 5

Thread: [2005] clear textbox will clear datagridview

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    [2005] clear textbox will clear datagridview

    hi! I have datagrid view and a sub to clear my textboxes. The first row of record in my datagrid will be erased from the datagridview (but not in my table) whenever i clicked other rows of my datagrid. I know it has to do with my cleartextbox sub.

    How do clear my textboxes with out erasing the records in my datagridview?

    Here's my code.
    Code:
        Private Sub frmSectionFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            cnn.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=NDDU-IBED;Integrated Security=True"
            If cnn.State = ConnectionState.Closed Then cnn.Open()
    
            cmdSection = cnn.CreateCommand
            cmdSection.CommandText = "SELECT * FROM tblSection"
            daSection.SelectCommand = cmdSection
            daSection.Fill(dsSection, "tblSection")
            cnn.Close()
    
            'bind datagrid view
            dtgSection.DataSource = dsSection.Tables("tblSection")
    
            'format header display
            dtgSection.Columns("SectionID").HeaderText = "Section ID"
            dtgSection.Columns("SectionName").HeaderText = "Section Name"
            dtgSection.Columns("Yearlevel").HeaderText = "Year Level"
    
            'bind textboxes
            txtSectionName.DataBindings.Add("Text", dsSection.Tables("tblSection"), "SectionName")
            txtYearLevel.DataBindings.Add("Text", dsSection.Tables("tblSection"), "YearLevel")
            txtSectionID.DataBindings.Add("Text", dsSection.Tables("tblsection"), "SectionID")
            ClearTextbox()
    
        Private Sub ClearTextbox()
            For Each ctl As Control In Me.Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Text = String.Empty
                End If
            Next ctl
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] clear textbox will clear datagridview

    You're binding data to the TextBoxes and then you're clearing the TextBoxes. That means that you are clearing the fields that are bound to the controls. I'm guessing that your intention is to not display a bound record but that's not what you're doing and it's not even possible. If you bind data then a bound item will be displayed. If you don't want to show any data then don't bind it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [2005] clear textbox will clear datagridview

    I would like to show data that's why I bound them. My concern would be to let the textbox clear so that when I Add records its ready to accept record rather than deleting what's in it.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] clear textbox will clear datagridview

    You don't clear the TextBoxes to add a new record. Like I said, the bound controls display the data from the current bound record. If you clear the Text of a bound TextBox then you are clearing the bound field of the current record, which is obviously not what you want to do. If you want to add a new record then you do exactly that: add a new record. The TextBoxes will then display the contents of that new record, which will be empty fields.

    What you should be doing is binding your data to the controls via a BindingSource. You then simply call the AddNew method of the BindingSource and voila! That will create a new DataRow for the underlying DataTable and display its contents in your TextBoxes. It's contents will, of course, be empty fields.

    Note that the row will not actually be added to the underlying DataTable until you either navigate away from the bound TextBoxes or else explicitly call EndEdit on the BindingSource. This is to allow you to discard the new row without adding it if you change your mind, either by pressing the Escape key or by explicitly calling CancelEdit on the BindingSource. It's to create this simplicity in data-binding for which the BindingSource was specifically created. Make use of it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [2005] clear textbox will clear datagridview

    thanks!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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