|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Datagridview...cell contentsin 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.... 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?ID=1139f14a-c236-4ad7-8882-b1ed16424252 There are more samples about that on our website. Cor Show quoteHide quote "Arne Beruldsen" <ArneBeruld***@discussions.microsoft.com> schreef in bericht news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... 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. -------------------------------------------------------------- Show quoteHide quote "Arne Beruldsen" <ArneBeruld***@discussions.microsoft.com> wrote in message news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... 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: Show quoteHide quote > 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" <ArneBeruld***@discussions.microsoft.com> wrote in message > news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... 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. ---------------------------------------------- Show quoteHide quote "Master Programmer" <master_program***@outgun.com> wrote in message news:1164701726.928928.223820@j44g2000cwa.googlegroups.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" <ArneBeruld***@discussions.microsoft.com> wrote in >> message >> news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... > 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 Show quoteHide quote "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" <ArneBeruld***@discussions.microsoft.com> wrote in message > news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... > > > 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 Show quoteHide quote "Arne Beruldsen" <ArneBeruld***@discussions.microsoft.com> schreef in bericht news:15CF9995-E7B6-4B75-A006-7586A89F6278@microsoft.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" <ArneBeruld***@discussions.microsoft.com> wrote in >> message >> news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... >> >> >> 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. Show quoteHide quote "Arne Beruldsen" <ArneBeruld***@discussions.microsoft.com> wrote in message news:15CF9995-E7B6-4B75-A006-7586A89F6278@microsoft.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" <ArneBeruld***@discussions.microsoft.com> wrote in >> message >> news:8271EF84-0CFB-462A-9B42-2650C216F393@microsoft.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.... >> >> >>
Newbie Question here
Newbie help: How to detect when a worker thread ihas completed normaly word automation vb.net Arranging window screens on multi-monitor Desktop set refresh rate for an active browser window How broad should an interface be? Advice needed. newbie syntax question: adding event handler for button not working File and Database Search Re: Is VB.NET Stable?? Error on deleting registry value |
|||||||||||||||||||||||