linking textbox to cell in a datagridview cell

C

cj

When viewing a datatable in a datagridview one of the columns in it is a
"note" field which can be quite long. I would like to have the note
field of the currently selected row of the datagrid display in a textbox
below the datgridview. I currently have it fixed so if you click on a
row the note field from that row is put in the textbox. I'd like this
to be automatic as they move through rows.

Also the note field is one of two fields that they will be allowed to
edit while viewing the datagridview. They'd of course want to edit it
in textbox. How do I get these edits to automatically be put back in
the datagridview/datatable. I'm thinking there should be some way to
set the datasource of the textbox to a cell or something so what is
edited in it is edited in the datagridview/datatable.
 
C

cj

been looking at the first part. I tried DataGridView1.RowEnter and
RowLeave events and both seemed to show the cell from the row I just
left! I don't get that. Both of them?! I can see rowleave as it might
grab the data from the cell as it leaves but rowenter should get the
cell from the row that it's entering.

RowStateChanged sadly apparently doesn't fire when moving from row to row.

Finally this seems to work:

Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e
As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles
DataGridView1.RowPostPaint
If Not IsDBNull(DataGridView1.CurrentRow.Cells(17).Value) Then
TextBox1.Text = DataGridView1.CurrentRow.Cells(17).Value
End If
End Sub

Comments?

Moving on to part 2 of my question in the AM. Hopefully someone will
have some ideas on that overnight that I can try.
 
J

Jack Jackson

When viewing a datatable in a datagridview one of the columns in it is a
"note" field which can be quite long. I would like to have the note
field of the currently selected row of the datagrid display in a textbox
below the datgridview. I currently have it fixed so if you click on a
row the note field from that row is put in the textbox. I'd like this
to be automatic as they move through rows.

Also the note field is one of two fields that they will be allowed to
edit while viewing the datagridview. They'd of course want to edit it
in textbox. How do I get these edits to automatically be put back in
the datagridview/datatable. I'm thinking there should be some way to
set the datasource of the textbox to a cell or something so what is
edited in it is edited in the datagridview/datatable.

Can't you just bind the textbox to the appropriate property in the
data source of the grid?
 
J

Jeffrey Tan[MSFT]

Hi Cj,

#1, I think DataGridView.RowEnter is more suitable for your requirement; in
this event, DataGridViewCellEventArgs.RowIndex should report the current
enterring row index. If not, can you provide a simple sample to demonstrate
your problem?

#2, Just as Jack suggested, you may also bind the TextBox to the same
datasource as the DataGridView. Winform databinding mechanisum will help to
ensure the data synchronization between these two controls.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

cj

That would be great but how would I bind the textbox to the 17th column
of the currently selected row in a datagrid?
 
C

cj

#1, I would think that too, but as I noted both rowenter and rowleave
give the same results.

#2, How do I bind the textbox to the value in column 17 of the currently
selected row in the datagridview?

My code:

Public Class Form1
Dim mydt As New DataTable

Dim MySqlConnection As New SqlClient.SqlConnection
Dim MySqlCommand As New SqlClient.SqlCommand
Dim MySqlAdapter As New SqlClient.SqlDataAdapter


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MySqlConnection.ConnectionString = "........."
MySqlCommand.CommandText = "select * from Checks"
MySqlCommand.Connection = MySqlConnection
MySqlAdapter.SelectCommand = MySqlCommand

mydt.Clear()
mydt.Columns.Clear()
DataGridView1.DataSource = Nothing

Try
MySqlAdapter.Fill(mydt)

For x As Int32 = 1 To 16
mydt.Columns(x).ReadOnly = True
Next
mydt.Columns(18).ReadOnly = True

Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try

DataGridView1.DataSource = mydt
DataGridView1.AutoResizeColumns()
End Sub

Private Sub DataGridView1_RowPostPaint(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs)
Handles DataGridView1.RowPostPaint
If Not IsDBNull(DataGridView1.CurrentRow.Cells(17).Value) Then
TextBox1.Text = DataGridView1.CurrentRow.Cells(17).Value
End If
End Sub

End Class
 
J

Jack Jackson

I would try:

textbox.DataBindings.Add("Text", datasourcename, "DataSourceProperty")

where datasourcename is what you set the datagrid's DataSource
property to and "DataSourceProperty" is the property that is bound to
the 17th column in the datagrid.
 
C

cj

ok so textbox1.databinding.add("Text", mydt, ?????????????

I'm not sure what else to do. Here is how I set the datagridview to the
datatable: DataGridView1.DataSource = mydt
 
J

Jeffrey Tan[MSFT]

Hi Cj,

Thanks for your feedback.

I think Jack has provided you the code for the binding. To be more
specific, you should set "DataSourceProperty" to the 17th column name in
the DataTable. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
 
J

Jack Jackson

Each column of the datagridview is bound to a property of mydt. The
third argument is the name of the property of mydt that is bound to
the 17th column of the datagridview.
 
J

Jeffrey Tan[MSFT]

Hi Cj,

It should be the 17th DataTable column name as you desired to bind.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
 
C

cj

ok, thanks, I understand now. I'll play with that some later. Right
now I'll stick with another method of solving my problem I have managed
to get working.
 
J

Jeffrey Tan[MSFT]

Ok, if you need further help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top