|
-
Apr 25th, 2008, 10:28 PM
#1
Thread Starter
PowerPoster
[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
-
Apr 25th, 2008, 10:38 PM
#2
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.
-
Apr 25th, 2008, 10:44 PM
#3
Thread Starter
PowerPoster
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.
-
Apr 25th, 2008, 11:45 PM
#4
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.
-
Apr 26th, 2008, 06:40 PM
#5
Thread Starter
PowerPoster
Re: [2005] clear textbox will clear datagridview
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|