|
OK. If the datagrid and the textboxes are both bound to the dataset in the same binding context, you don't need any of this code.
All it takes is something like this:
'
' Code to connect to database and fill a DataTable, or DataSet.
'
' Setup the bindings...
BindingSource1.DataSource = myDataTable
DataGridView1.DataSource = BindingSource1
SomeTextBox1.DataBindings.Add("Text", BindingSource1, "SomeColumnName")
SomeTextBox2.DataBindings.Add("Text", BindingSource1, "SomeColumn")
SomeTextBox3.DataBindings.Add("Text", BindingSource1, "OtherColumnName")
That's it, no other code to set anything really. Unless you're doing something that isn't so straight forward??
|
|
|
|
|
Dave Kreskowiak wrote: That's it, no other code to set anything really. Unless you're doing something that isn't so straight forward??
Im not doing anything that isnt straight forward but when the user types into the textbox, without moving to another row how is the data refreshed and saved without doing something like I did?
CleaKO
"Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)
|
|
|
|
|
So long as the textbox's are bound in the same binding context, managed by the bindingsource) as the datagrid, you don't have to do anything. It happens automatically!
Try it! Start a new project. Create a form with a DGV and a couple of textboxs on it and add a bindingsource object to the form. Write some SQL to fill a datatable with a few columns of data and bind the BindingSource to the DataTable. Bind the DGV and the TextBox's to the BindingSource, like in my previous post, and try it! It might just surprise you.
|
|
|
|
|
I have Edit mode and Add mode, when the user chooses Add mode I clear the textbox bindings and when the user chooses Edit mode I use the following code.
uxFirstName.DataBindings.Add("Text", BindingSourceCirculars, "FirstName", True)
uxLastName.DataBindings.Add("Text", BindingSourceCirculars, "LastName", True)
uxAddress.DataBindings.Add("Text", BindingSourceCirculars, "Address", True)
uxCity.DataBindings.Add("Text", BindingSourceCirculars, "City", True)
uxState.DataBindings.Add("Text", BindingSourceCirculars, "State", True)
uxZipCode.DataBindings.Add("Text", BindingSourceCirculars, "ZipCode", True)
Unless I choose the find the row and edit it, nothing in the DataGridView changes until I change rows in the grid. Am I missing something? Binding seems to be much more complicated in 2.0 than it EVER was in 1.1 and I cant seem to find the information I need in order to make this work the way I want it to.
CleaKO
"Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)
|
|
|
|
|
Is the real trick here to just call BindingSource.EndEdit in order to have that change applied if I am trying to have the user push a "Save" button for familiarity purposes?
CleaKO
"Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)
|
|
|
|
|
Did ya try it? It's very possible that's all it's going to take. On top of that, maybe refreshing the DGV.
|
|
|
|
|
Can someone tell me the difference between these similar dataset writing routines?
The data writing subroutine:
Private Sub New_Bindings(ByVal intOrdinal As Integer, _
ByVal strCode As String, _
ByVal strName As String)
drCurrent = MyDataSet.Tables("tblBindings").NewRow()
drCurrent!Ordinal = intOrdinal
drCurrent!BindingCode = strCode
drCurrent!BindingName = strName
MyDataSet.Tables("tblBindings").Rows.Add(drCurrent)
Call Show_Changes("Sub New_Bindings", MyDataSet)
End Sub
After each row is written into the dataset I have another subroutine that prints out the results called Show_Changes.
Case one – The data is placed directly into the subroutine call.
I’m writing 5 rows to the dataset like this:
Call New_Bindings(1, "TST1", "Record 1")
Call New_Bindings(2, "TST2", "Record 2")
Call New_Bindings(3, "TST3", "Record 3")
Call New_Bindings(4, "TST4", "Record 4")
Call New_Bindings(5, "TST5", "Record 5")
Output screen results after each row addition:
Sub - Sub New_Bindings
TableName: tblBindings
1 TST1 1 Record 1
Sub - Sub New_Bindings
TableName: tblBindings
1 TST1 1 Record 1
2 TST2 2 Record 2
Sub - Sub New_Bindings
TableName: tblBindings
1 TST1 1 Record 1
2 TST2 2 Record 2
3 TST3 3 Record 3
Sub - Sub New_Bindings
TableName: tblBindings
1 TST1 1 Record 1
2 TST2 2 Record 2
3 TST3 3 Record 3
4 TST4 4 Record 4
Sub - Sub New_Bindings
TableName: tblBindings
1 TST1 1 Record 1
2 TST2 2 Record 2
3 TST3 3 Record 3
4 TST4 4 Record 4
5 TST5 5 Record 5
This works just fine. All the records have been written to the dataset and I can use the BindingNavigator control to go from record to record.
Case 2 – The data is placed into the subroutine via variables.
Call New_Bindings(CInt(txtOrdinal.Text), txtBindingCode.Text, txtBindingName.Text)
Output screen after the same five rows are added.
Sub - Sub New_Bindings
TableName: tblBindings
1 TST1 1 Record 1
Sub - Sub New_Bindings
TableName: tblBindings
1 TST2 2
2 TST2 2 Record 2
Sub - Sub New_Bindings
TableName: tblBindings
1 TST3 3
2 TST2 2 Record 2
3 TST3 3 Record 3
Sub - Sub New_Bindings
TableName: tblBindings
1 TST4 4
2 TST2 2 Record 2
3 TST3 3 Record 3
4 TST4 4 Record 4
Sub - Sub New_Bindings
TableName: tblBindings
1 TST5 5
2 TST2 2 Record 2
3 TST3 3 Record 3
4 TST4 4 Record 4
5 TST5 5 Record 5
As you can see the dataset is not the same. The method of data entry is the same except that case 2 uses variables instead of literals. Obviously .NET handles these two similar data entry methods differently, but I haven’t a clue as to how or why this occurs.
Here is the entire code for this one form:
Option Explicit On
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlTypes
Imports System.Data.SqlDbType
Imports System.Data.SqlClient
Public Class frmBindings
Dim Msg As String
Dim strSQL As String
'Declare the SQLDataAdapter and DataSet objects
Dim daBindings As New SqlDataAdapter()
Dim objCommandBuilder As New SqlCommandBuilder(daBindings)
Dim MyDataSet As New DataSet()
Dim changesDataSet As DataSet
Dim drCurrent As DataRow
Private Function Is_Filled_Out() As Boolean
Dim b1 As Boolean, b2 As Boolean, b3 As Boolean
If (txtOrdinal.Text = Nothing) Then
txtOrdinal.BackColor = Color.Yellow
b1 = False
Else
txtOrdinal.BackColor = Color.White
b1 = True
End If
If (txtBindingCode.Text = Nothing) Then
txtBindingCode.BackColor = Color.Yellow
b2 = False
Else
txtBindingCode.BackColor = Color.White
b2 = True
End If
If (txtBindingName.Text = Nothing) Then
txtBindingName.BackColor = Color.Yellow
b3 = False
Else
txtBindingName.BackColor = Color.White
b3 = True
End If
Is_Filled_Out = b1 And b2 And b3
End Function
Private Sub Clear_Form()
txtBindingID.Text = Nothing
txtBindingCode.Text = Nothing
txtBindingName.Text = Nothing
txtOrdinal.Text = Nothing
txtOrdinal.Focus()
End Sub
Private Sub Load_DataSet()
'Connect to the SQL Server database
Dim cn As SqlConnection = New SqlConnection(My.Settings. _
PrintsByMe_DevConnectionString)
cn.Open()
'Retrieve the data using a SQL statement
strSQL = "SELECT * FROM [tblBindings];"
Dim cd As New SqlCommand(strSQL, cn)
'Set the database connection for MySqlDataAdapter
daBindings.SelectCommand = cd
'Fill the DataSet and DataSet Schema
daBindings.MissingSchemaAction = MissingSchemaAction.AddWithKey
daBindings.FillSchema(MyDataSet, SchemaType.Source, "tblBindings")
daBindings.Fill(MyDataSet, "tblBindings")
'Close database connection
cn.Close()
MyDataSet.Tables(0).Columns("BindingID").AutoIncrement = True
MyDataSet.Tables(0).Columns("BindingID").AutoIncrementSeed = 1
MyDataSet.Tables(0).Columns("BindingID").AutoIncrementStep = 1
'Set the BindingNavigator's DataSource to the DataSet we created.
bsBindings.DataSource = MyDataSet
'Set the BindingSource Datamember to the table we are using.
bsBindings.DataMember = MyDataSet.Tables(0).TableName()
'Bind form control txtBindingID to the BindingID field
txtBindingID.DataBindings.Add("Text", bsBindings, "BindingID")
'Bind form control txtOrdinal to the Ordinal field
txtOrdinal.DataBindings.Add("Text", bsBindings, "Ordinal")
'Bind form control txtBindingCode to the BindingCode field
txtBindingCode.DataBindings.Add("Text", bsBindings, "BindingCode")
'Bind form control txtBindingName to the BindingName Field
txtBindingName.DataBindings.Add("Text", bsBindings, "BindingName")
End Sub
Private Sub New_Bindings(ByVal intOrdinal As Integer, _
ByVal strCode As String, _
ByVal strName As String)
drCurrent = MyDataSet.Tables("tblBindings").NewRow()
drCurrent!Ordinal = intOrdinal
drCurrent!BindingCode = strCode
drCurrent!BindingName = strName
MyDataSet.Tables("tblBindings").Rows.Add(drCurrent)
Call Show_Changes("Sub New_Bindings", MyDataSet)
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmBindings_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Call Load_DataSet()
naviBindings.BindingSource = bsBindings
End Sub
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BindingNavigatorAddNewItem.Click
Call New_Bindings(CInt(txtOrdinal.Text), txtBindingCode.Text, txtBindingName.Text)
Call Clear_Form()
End Sub
Private Sub BindingNavigatorUpdateDB_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BindingNavigatorUpdateDB.Click
Dim intModified As Integer
Dim intAdded As Integer
Dim intRes As Integer
If MyDataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) Then
changesDataSet = MyDataSet.GetChanges(DataRowState.Modified Or DataRowState.Added)
'Call Show_Changes(changesDataSet)
intModified = Modified_Records(changesDataSet)
intAdded = Added_Records(changesDataSet)
Msg = "You have modified " & CStr(intModified) & " records and " & _
"have added " & CStr(intAdded) & " records." & _
vbCRLF & "Do you wish to commit these changes to the database?"
intRes = MsgBox(Msg, MsgBoxStyle.Information + MsgBoxStyle.YesNo, "DataSet Changes")
If intRes = vbYes Then
'daBindings.Update(MyDataSet, "tblBindings")
Call Show_Changes("BindingNavagatorUpdateDB", changesDataSet)
Else
Msg = "Changes aborted." & vbCRLF & "The DataSet has NOT updated the database."
End If
End If
End Sub
Private Sub BindingNavigatorSaveNewItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BindingNavigatorSaveNewItem.Click
If Is_Filled_Out() Then
Call New_Bindings(CInt(txtOrdinal.Text), txtBindingCode.Text, txtBindingName.Text)
Call Clear_Form()
Else
Msg = "Please fill in the highlighted box(es)."
MsgBox(Msg, MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
End If
'txtOrdinal.Focus()
'Call New_Bindings(1, "TST1", "Record 1")
'Call New_Bindings(2, "TST2", "Record 2")
'Call New_Bindings(3, "TST3", "Record 3")
'Call New_Bindings(4, "TST4", "Record 4")
'Call New_Bindings(5, "TST5", "Record 5")
End Sub
End Class
Thanks,
Quecumber256
|
|
|
|
|
While you Post you got a warniing abt this message is too long?
so please post what is your problem on which line you got problem ?
Be specific to your question?
So that we can answer easily to solve your problem.
Regards,
Satips.
|
|
|
|
|
Satips:
I don't know exactly where the application fails. There are two different methods of entering data that should produce the same results, but don't.
Cleako asked for the code on Friday 1 Jun 07 when I first posted it. Today I reposted the question showing exactly what is happening and attached the code at the bottom of the letter so if some one needed the code they could look at it.
Do I need to get with Microsoft for a resolution to this problem?
Quecumber256
|
|
|
|
|
I asked for the Binding portion, I apologize for not getting back with you. I honestly dont know why you are having an issue with this because it looks correct, it looked correct before, and unless you changed it Im sure it's still correct. What you are doing is one of the most basic functions of ADO.NET.
CleaKO
"Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)
|
|
|
|
|
Thank you for reassuring me I wasn't a dummy .
I attached the entire form class just in case there was an issue with the data binding portion or something else.
I have one other resource looking it over. If he can't find the problem then I'm being forced to use one of my complementry support intances.
Thank you,
Quecumber256
|
|
|
|
|
in my project is asp.net with c# project but my vss schedular is asp.net with vb.net.
Every this schedular is running on night 12.00am .
In that schedular i have somany doc files and excel files,.pdf files.
So while schedular running time html paths and actual document paths but some times it automatically stopped then i want do if any document contains problem then schedular ignore's that document and goes to next document.For this i want code. Iam sending some of the classes of the my project.
public function get vss connection()
Public Function PopulateProject() As Boolean
Private Function PopulateSubProject(ByVal sParent As String, ByVal ItemIsDeleted As Boolean) As Boolean
Private Function Traverse(ByVal sProjectPath As String, ByVal SWFPath As String)
Private Function PopulateFiles(ByVal sParentID As Long, ByVal sProjectPath As String, ByVal sFolderPath As String) As Boolean
Private Sub InsertMenuRights()
Private Sub InsertDocNames()
Private Sub CreateFolder(ByVal Path As String)
Private Function GetNWExt(ByVal FileName As String) As String
Public Sub Conversion()
Private Function GetHTMLPath(ByVal FilePath As String)
Private Sub DocToHTML(ByVal FilePath As String, ByVal HTMLPath As String)
Private Sub PptToHTML(ByVal FilePath As String, ByVal HTMLPath As String)
Private Sub PDFToHTML(ByVal FilePath As String, ByVal HTMLPath As String)
Private Sub ExcelToHTML(ByVal FilePath As String, ByVal HTMLPath As String)
Private Function GetTPath(ByVal DocPath As String)
Private Function GetTruncPath(ByVal HTMPath As String)
where i will mention code in this classes.Please send me code as soon as possible my id is samsun125@gmail.com.
samrama
|
|
|
|
|
You expect people from the forum to write your code? Perhaps you should try to start to write your own code, then if you run into specific problems ask a question and we will try to help you with your questions.
Ben
|
|
|
|
|
srinivassam wrote: Please send me code as soon as possible my id is samsun125@gmail.com.
Please send me $50,000 as soon as possible. Your code will follow.
Another rentacoder buyer is about to be very disappointed...
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
hi guys
The problem im having is datagridview rowindex matches the binding dataset.table index, as long as I don't click on one of the column headers to change the datagridview sort order. can anyone help me getting rowindex to be the same no matter if i sort the DGV or not.
Thanks in advance
Robbo
|
|
|
|
|
You normally wouldn't care about the row index in the DGV. What are you trying to do with it?
|
|
|
|
|
Hi Dave
thanks for your reply.
basically my DGV has only a few bound columns and what i intend to do is retrieve further data from table based on the select DGV row.
so if rowindex 0 is selected from the grid i want to be able to talk to the table based on the same index making sure its the same record. i dont want to add more bound columns in the grid because there are too much.
any tips or advice would be appreciated.
Robby
|
|
|
|
|
vbbeg wrote: what i intend to do is retrieve further data from table based on the select DGV row.
OK. Then you're looking for the SelectedRows or SelectedCells. They return a DataGridViewRow and DataGridViewCell respectively. These will contain the data that the user clicked on, regardless if the DGV is bound or not, sorted or not.
Private Sub DGV1_SelectionChanged(blah, blah) Handles DGV1.SelectionChanged
For Each row As DataGridViewRow In DGV1.SelectedRows
For Each cell As DataGridViewCell In row.Cells
Debug.Write(cell.FormattedValue.ToString() & " | ")
Next
Debug.WriteLine(String.Empty)
Next
End Sub
Each DataGridViewCell object knows which DataGridViewRow contains it, through its OwningRow property. If the user clicks on any cell, the OwningRow property of that cell will give you the DataGridViewRow that the user clicked in. Using that, you can get the ID column of the row and use that value any way you want.
For Each cell As DataGridViewCell In DGV1.SelectedSells
Debug.WriteLine(String.Format("Cell Data: {0} Parent Row ID: {1}", _
cell.FormattedValue.ToString, _
cell.OwningRow.Cells("IDColumnName").FormattedValue.ToString))
Next
|
|
|
|
|
Thank for your patience dave.
I just need clarification on something.. realisticly what im trying to do is clone a record and rewrite columns so that essentially its not a duplicated record after all when its written to a table but I havent been able to work that out. (any advice you can give me will save you reading below and helping me along where im at right now)..
so imagine in my DGV i have surname , firstname , address . if i select a particular record showing these columns and rows respectively i want to additionally go to the table and also get "phone number" etc.
i guess what im asking is based on the DGV selection how can i go to the table and pickout the rest of the fields i need for that same row of data selected in the DGV.
e.g. SELECTED ROW IN DGV(IN BOLD)
[SURNAME]-[FIRSTNAME]-[ADDRESS]
CHAN,...........ALEX,.......mamre drive.............[AT INDEX 0}
Bloggs,.......JOE,.......2 Hollywood st , hollywood..INDEX 1
if i sort this by FIRSTNAME IN DESCENDING. it would be
[SURNAME]-[FIRSTNAME]-[ADDRESS]
Bloggs,.........JOE,.....2 Hollywood st , hollywood... INDEX 0
CHAN,.............ALEX,.....mamre drive .....................INDEX 1
im trying to use the below line
where "i" is the row number coming from DGV select row index.
dim value as string
value = ds.tables("customers").rows(i).item("phone")
* if i use the index from the DGV after i sort it it doesnt return the correct record to match the phone number...
just a reminder.. If i dont sort the DGV at all. I can use the DGV.index and use it here :
dim value as string
value = ds.tables("customers").rows(i).item("phone") it WORKS CORRECTLY. its just after i sort the DGV that I cant get
ds.tables("customers").rows(i).item("phone") to return the correct row from the table..
sorry about this dave .. thanks for your patience.
Robbo
|
|
|
|
|
You do not need the row number at all. Once you have to DataGridViewRow, you have the ENTIRE record that was clicked on. Every field that the grid was bound to in is that row object. So
Dim phone as String = cell.OwningRow.Cells("phone").Value
gives you the Phone column of the row that was clicked on. The example, of course, assumes that the Value property in that column returns a String. You'll have to change it to match whatever you're expecting.
You don't need to go to the table to get the rest of the fields.
|
|
|
|
|
If you didn't retrieve all the data you wanted in the first fetch, you need to change the first fetch to include the primary key in the table you bound the DGV to. When the use clicks on a row, you grab the PK value from the DataRow object and use that value in a second request to get the columns you need specifying the PK in the WHERE clause of the SQL SELECT.
|
|
|
|
|
Thankyou Dave. I think now where getting somewhere. ill let you know how i go later on . thanks champ.
|
|
|
|
|
Hi
I am trying to add *.dll files as a reference in VB.NET 2005 but am recieveing the message as 'A reference (Path) could not be added please make sure that the file is accessible and that it is a valid assembly or COM components'.
Help me to proceed......
Thanks,
Nanda
|
|
|
|
|
hi,
if you want to add reference to project:
go in solution explorer and you will find Reference and click right on Reference and choose Add Reference and after that you can choose your reference
hope it helped.
|
|
|
|
|
Hi,
am recieving the error message while adding the reference(i can't able to add that dll), please help in this
Error message:
'A reference (Path) could not be added please make sure that the file is accessible and that it is a valid assembly or COM components'.
Thanks,
Nanda
|
|
|
|
|