473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

linking textbox to cell in a datagridview cell

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.
Dec 4 '07 #1
12 13559
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.DataGridViewRowPostPaintEvent Args) 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.

cj wrote:
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.
Dec 4 '07 #2
On Tue, 04 Dec 2007 15:51:31 -0500, cj <cj@nospam.nospamwrote:
>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?
Dec 5 '07 #3
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 5 '07 #4
cj
That would be great but how would I bind the textbox to the 17th column
of the currently selected row in a datagrid?

Jack Jackson wrote:
On Tue, 04 Dec 2007 15:51:31 -0500, cj <cj@nospam.nospamwrote:
>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?
Dec 5 '07 #5
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.DataGridViewRowPostPaintEvent Args)
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
Jeffrey Tan[MSFT] wrote:
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 5 '07 #6
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.

On Wed, 05 Dec 2007 08:20:33 -0500, cj <cj@nospam.nospamwrote:
>That would be great but how would I bind the textbox to the 17th column
of the currently selected row in a datagrid?

Jack Jackson wrote:
>On Tue, 04 Dec 2007 15:51:31 -0500, cj <cj@nospam.nospamwrote:
>>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?
Dec 5 '07 #7
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

Jack Jackson wrote:
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.

On Wed, 05 Dec 2007 08:20:33 -0500, cj <cj@nospam.nospamwrote:
>That would be great but how would I bind the textbox to the 17th column
of the currently selected row in a datagrid?

Jack Jackson wrote:
>>On Tue, 04 Dec 2007 15:51:31 -0500, cj <cj@nospam.nospamwrote:

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?
Dec 6 '07 #8
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

Dec 6 '07 #9
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.

On Wed, 05 Dec 2007 22:26:26 -0500, cj <cj@nospam.nospamwrote:
>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

Jack Jackson wrote:
>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.

On Wed, 05 Dec 2007 08:20:33 -0500, cj <cj@nospam.nospamwrote:
>>That would be great but how would I bind the textbox to the 17th column
of the currently selected row in a datagrid?

Jack Jackson wrote:
On Tue, 04 Dec 2007 15:51:31 -0500, cj <cj@nospam.nospamwrote:

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?
Dec 6 '07 #10
Hi Cj,

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

Best regards,
Jeffrey Tan
Microsoft Online Community Support
Dec 6 '07 #11
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.

Jeffrey Tan[MSFT] wrote:
Hi Cj,

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

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Dec 6 '07 #12
Ok, if you need further help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Dec 7 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DraguVaso | last post by:
Hi, I'm looking for a nice way to format the cell of my datagrid according to some predefined 'rules'. I found a lot on the site of George shepherd, but it wasn't really what I was looking for:...
8
by: jaYPee | last post by:
i have downloaded the sample of cell by cell validation from this site http://www.syncfusion.com/faq/winforms/search/773.asp but it is a c sharp. anyone have a vb version of this sample program?
0
by: Steve | last post by:
I am looking for an example of a custom user control ( 2 labels and 2 datetime pickers ) in a datagridview cell. Every example I have come across handles only once control such as a datetimepicker...
11
by: dgk | last post by:
Is there a way to change the foreground or background color of a single cell in an unbound datagridview?
3
by: Rich | last post by:
Hello, If I want to update data displayed in a datagrideview/datagridview cell, how can I determine what cell I am updating? I am looking at the click event below, for example. Can I get...
0
by: varunk2 | last post by:
Hi How to autocomplete a datagridview cell in c# windows forms. Please anybody help me... Thanks in advance Regards,
0
by: hydro123 | last post by:
I am using VC++2008 and am trying to read data enetered in unbound datagridview to implement in function. Under button_click event I entered the following: // initialize varaibles from...
0
by: ziketo | last post by:
Hi, I searched a lot about changing the DataGridView cell borders. BYTES helped me so I will write down the solution: 1. You should override the class DataGridViewTextBoxCell, and the new class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.