473,386 Members | 1,791 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,386 software developers and data experts.

Datagridview...cell contents

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
7 3088
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Satya | last post by:
Hi, I am using a DataGridView to display a large amount of data and so need to use filters for displaying certain data. For this I want to provide a textbox for each column of the DataGridView so...
8
by: George | last post by:
Hi, I have been trying to see if I can do the following: 1. Create a DataGridView 2. Create 2 columns in the DataGridView (1 textbox and the other combobox) 3. Create a DataTable containing...
4
by: gregarican | last post by:
I have a standard VC# 2005 DataGridView showing a group of records coming from a table. Currently I have a CellMouseDoubleClick event popping up a MessageBox which pulls a timestamp of the...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
0
by: ajanu81 | last post by:
hi all, i am new to this forum. i am using a windows application using C#. I am using a datagridview to display contents of a xml file. say i have a xml file like this. <xml version="1.0"...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
5
by: BabliS1 | last post by:
I am using Vb.net2005. I have a datagridview which displays some data. I want to filter the records based on some criteria given by the user at runtime. How can I do this. Please Help. Thanks in...
0
by: priyamtheone | last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have: 1) Initially all the cells of the dtp column should be...
1
by: Avedo | last post by:
Hello! I have a form application with a DataGridView, and an Save/Delete and Cancel button. When the application is opened, it is supplied an XML file that may or may not exist, and display the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.