By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Datagridview...cell contents

P: n/a
in vbnet2005 I have a datagridview. When the user clicks on a row...I would
like the contents of certain cells to populate a textbox. To do this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....
Nov 28 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Arne,

It is very important how you populate your datagridview. If that is with a
datasource than mostly using the bindingmanager is the nicest way.

In this sample that is again very nice showed.
http://www.vb-tips.com/dbpages.aspx?...2-b1ed16424252

There are more samples about that on our website.

Cor
"Arne Beruldsen" <Ar***********@discussions.microsoft.comschreef in
bericht news:82**********************************@microsof t.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....

Nov 28 '06 #2

P: n/a
If you are populating the textbox with the same columns all the time,
you can bind your datagrid to a BindingSource, then bind the
BindingSource to the DataSet.

Then you can bind the textbox to the same BindingSource. I
believe if you do this, when you pick an entry in the grid, it will
automatically update the textbox with the value in that row.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = nwData
m_CustomersGrid.DataMember = "Customers"

Dim table as DataTable = nwData.Customers
m_CustomerTextBox.DataBindings.Add("Text", table, "CustomerID", True)

Robin S.
--------------------------------------------------------------

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....

Nov 28 '06 #3

P: n/a
Databinding is for children. Why not learn to do it the proper way -
programaticaly.

MS are always pushing that databinding crap (even in VB 6.0). I never
trust or want it. Better to write some code yourself to populate the
grid manually, instead of trusting the crappy VS wizards and
databinding features.

Steve Ray Irwin
RobinS wrote:
If you are populating the textbox with the same columns all the time,
you can bind your datagrid to a BindingSource, then bind the
BindingSource to the DataSet.

Then you can bind the textbox to the same BindingSource. I
believe if you do this, when you pick an entry in the grid, it will
automatically update the textbox with the value in that row.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = nwData
m_CustomersGrid.DataMember = "Customers"

Dim table as DataTable = nwData.Customers
m_CustomerTextBox.DataBindings.Add("Text", table, "CustomerID", True)

Robin S.
--------------------------------------------------------------

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....
Nov 28 '06 #4

P: n/a
Robin...

I added this line of code at the Cell Click Event...

TextEdit11.DataBindings.Add("Text", SxTable, "LastName", True)

Two problems...
1. It takes the "LastName" from the first record and not from the cell
selected
2. If I hit another cell....I get this error message "This causes two
bindings in the collection to bind to the same property."

Thanks for your help...

Arne

"RobinS" wrote:
If you are populating the textbox with the same columns all the time,
you can bind your datagrid to a BindingSource, then bind the
BindingSource to the DataSet.

Then you can bind the textbox to the same BindingSource. I
believe if you do this, when you pick an entry in the grid, it will
automatically update the textbox with the value in that row.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = nwData
m_CustomersGrid.DataMember = "Customers"

Dim table as DataTable = nwData.Customers
m_CustomerTextBox.DataBindings.Add("Text", table, "CustomerID", True)

Robin S.
--------------------------------------------------------------

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....


Nov 28 '06 #5

P: n/a
That is true of versions prior to VB2005. MS has substantially
improved the data binding capabilities, and they actually work
now. In some cases, it's useful, and in some cases, you don't
want to use it because you want more control. It just depends
on the situation.

Robin S.
----------------------------------------------
"Master Programmer" <ma***************@outgun.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
Databinding is for children. Why not learn to do it the proper way -
programaticaly.

MS are always pushing that databinding crap (even in VB 6.0). I never
trust or want it. Better to write some code yourself to populate the
grid manually, instead of trusting the crappy VS wizards and
databinding features.

Steve Ray Irwin
RobinS wrote:
>If you are populating the textbox with the same columns all the time,
you can bind your datagrid to a BindingSource, then bind the
BindingSource to the DataSet.

Then you can bind the textbox to the same BindingSource. I
believe if you do this, when you pick an entry in the grid, it will
automatically update the textbox with the value in that row.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = nwData
m_CustomersGrid.DataMember = "Customers"

Dim table as DataTable = nwData.Customers
m_CustomerTextBox.DataBindings.Add("Text", table, "CustomerID", True)

Robin S.
--------------------------------------------------------------

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in
message
news:82**********************************@microso ft.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do
this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....

Nov 28 '06 #6

P: n/a
Arne,

If you read Add in Net you can be surethat it is about a collection.

Therefore you are adding everytime a binding at every click.
The way you solve that is up to you, either outside your method, using a
test if the collection is more than 1, or use a static boolean. (I prefer
the first one).

Secondly don't forget to do a bidingsource EndEdit (version 2.0) or a
currencymanager EndCurrentEdit (version 1.1), to push the data down at the
click event, this is only done automaticly at a row change, but that is not
with a textbox.

I hope this helps a little bit.

Cor

"Arne Beruldsen" <Ar***********@discussions.microsoft.comschreef in
bericht news:15**********************************@microsof t.com...
Robin...

I added this line of code at the Cell Click Event...

TextEdit11.DataBindings.Add("Text", SxTable, "LastName", True)

Two problems...
1. It takes the "LastName" from the first record and not from the cell
selected
2. If I hit another cell....I get this error message "This causes two
bindings in the collection to bind to the same property."

Thanks for your help...

Arne

"RobinS" wrote:
>If you are populating the textbox with the same columns all the time,
you can bind your datagrid to a BindingSource, then bind the
BindingSource to the DataSet.

Then you can bind the textbox to the same BindingSource. I
believe if you do this, when you pick an entry in the grid, it will
automatically update the textbox with the value in that row.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = nwData
m_CustomersGrid.DataMember = "Customers"

Dim table as DataTable = nwData.Customers
m_CustomerTextBox.DataBindings.Add("Text", table, "CustomerID", True)

Robin S.
--------------------------------------------------------------

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in
message
news:82**********************************@microso ft.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do
this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....



Nov 29 '06 #7

P: n/a

Just to make sure I understand what you're going for:
You have a grid and a textbox. You want to be able
to click on a row in the grid and have it show one
of the fields in the textbox with the value corresponding
to the selected row.

Okay, you asked for it, here's the giant example. I got
this from Brian Noyes' book on Data Binding.

For this example, I have a CustomersDataSet defined.
It contains the Customers table in the Northwind database.

I have a Grid that is going to be bound to the CustomersDataSet
called CustomersDataGridView.

I have textboxes for CustomerID, ContactName, CompanyName,
and ContactPhone.

I also have a combobox for ContactNames.

I have ONE binding source: CustomersBindingSource.

When you change the row selected in the grid, it populates
the textboxes with the corresponding data. The combobox
for ContactNames shows the corresponding contact name.

I also have buttons on the screen for moving through the
records, and a textbox for the record# (position). If you
move through the records that way, it changes the position
in the grid, and changes the textboxes and combobox
accordingly.

If they change the ContactName in the combobox, it
positions the grid at the record that matches it, and
changes the textboxes accordingly.

This runs against NorthWind, so you could theoretically
hook this up and run it if you have that database, if you
set up the screen with the same controls (you should be
able to figure out their names by the code below).

---------------------------------------------------------------------
Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

'Set up event handlers for connector position changed.
AddHandler CustomersBindingSource.PositionChanged, _
AddressOf OnPositionChanged
AddHandler PositionTextBox.TextChanged, _
AddressOf OnPositionTextChanged

'Add event handlers for the buttons; you could do this
' by setting the properties on the buttons, but I'd
' rather do it specifically
AddHandler MoveFirstButton.Click, AddressOf OnFirstRecord
AddHandler MovePreviousButton.Click, AddressOf OnPreviousRecord
AddHandler MoveNextButton.Click, AddressOf OnNextRecord
AddHandler MoveLastButton.Click, AddressOf OnLastRecord

'Set up data bindings.
'First, populate the dataset.
Dim nwData As CustomersDataSet = CustomersDataSet.GetCustomers()

'Set the data source for the binding source to the dataset.
CustomersBindingSource.DataSource = nwData
'Add this so it knows which table to use .
CustomersBindingSource.DataMember = "Customers"
'Set the data source for the grid to the customers binding source.
CustomersDataGridView.DataSource = CustomersBindingSource

'Add the bindings for the controls on the form.
AddTextBoxDataBindings()
AddComboBoxDataBindings()

End Sub

Private Sub AddTextBoxDataBindings()
'Bind each text box to the appropriate field
' in the CustomersBindingSource
CustomerIDTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CustomerID")
ContactNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "ContactName")
CompanyNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CompanyName")
ContactPhoneTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "Phone")

End Sub

Private Sub AddComboBoxDataBindings()
ContactsComboBox.DataSource = CustomersBindingSource
ContactsComboBox.DisplayMember = "ContactName"
ContactsComboBox.ValueMember = "CustomerID"
End Sub

Private Sub OnPositionChanged(ByVal sender As Object, _
ByVal e As EventArgs)
PositionTextBox.Text = CustomersBindingSource.Position.ToString
End Sub

Private Sub OnPositionTextChanged(ByVal sender As Object, _
ByVal e As EventArgs)
Dim enteredPos As Integer
'If it parses to an integer successfully, change the position.
Dim success As Boolean = Integer.TryParse(PositionTextBox.Text, _
enteredPos)
If success Then
CustomersBindingSource.Position = enteredPos
End If
End Sub

Private Sub OnFirstRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MoveFirst()
End Sub

Private Sub OnPreviousRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MovePrevious()
End Sub

Private Sub OnNextRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MoveNext()
End Sub

Private Sub OnLastRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MoveLast()
End Sub
---------------------------------------------------------------------------------
Hope this helps.
Robin S.

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
Robin...

I added this line of code at the Cell Click Event...

TextEdit11.DataBindings.Add("Text", SxTable, "LastName", True)

Two problems...
1. It takes the "LastName" from the first record and not from the cell
selected
2. If I hit another cell....I get this error message "This causes two
bindings in the collection to bind to the same property."

Thanks for your help...

Arne

"RobinS" wrote:
>If you are populating the textbox with the same columns all the time,
you can bind your datagrid to a BindingSource, then bind the
BindingSource to the DataSet.

Then you can bind the textbox to the same BindingSource. I
believe if you do this, when you pick an entry in the grid, it will
automatically update the textbox with the value in that row.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = nwData
m_CustomersGrid.DataMember = "Customers"

Dim table as DataTable = nwData.Customers
m_CustomerTextBox.DataBindings.Add("Text", table, "CustomerID", True)

Robin S.
--------------------------------------------------------------

"Arne Beruldsen" <Ar***********@discussions.microsoft.comwrote in
message
news:82**********************************@microso ft.com...
in vbnet2005 I have a datagridview. When the user clicks on a row...I
would
like the contents of certain cells to populate a textbox. To do
this...i
need to be able to refer to the row and colums...something like
textbox = contents (rx, cx)

How can I refer to the cell contents?

Thanks....



Nov 29 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.