data from Gridview to textbox - vb.net

i have a form with datagridview in vb.net that show my data in columns. what i'm trying to accomplish is that after choosing a row and pressing Edit button a new form will open and split the row for the right text boxes to update the data. the datagridview row shows different types of data: name,email,date,etc... any idea? Thanks in advance!

In the EditButton click-handler, you may access the selected row in your datagridview with the MyDataGridView.CurrentRow property.
This object - a DataGridViewRow - has Cells, which you can access individually by index and then get to their values:
...CurrentRow.Cells(n).Value
This then, in turn, you may use to fill the items in your edit form.
After completing your edit form, retrieve the (updated) values and put them back into the CurrentRow.Cells(n).Value
If your datagridview is databound, then you can also work directly with the datastore, through
...CurrentRow.DataBoundItem
The type of this object depends of course on your configuration; it may, for instance, be a DataRow.
This should be enough to get you going.
Final note on your added remark "thanks ...": you're apparently new at this site. I'm not sure why you got "downvotes". It may be because your question is rather elementary ("homework"), or people found your problem description not clear/detailed enough. Please, understand that answers (if any) are provided "unpaid for", and require "voluntary" effort from the contributors. Understandably they prefer that YOU do most of the work/study/trying, before asking someone else "to do your work for you". No offense intended.

Related

VB.Net ComboBox (as Dropdown) not translating text to DisplayMember with Databinding

I inherited a fairly large project at work that is undocumented and written in VB (originally started pre .NET, ended around .NET 2). I'm in the process of updating / refreshing a lot of the code, but have run into an annoying issue that I haven't found the solution for yet. This system utilizes a UI, a Web Service, and a SQL DB.
Problem: I have a Databound Combobox (originally set to DropDownList - I'm changing it to DropDown, which is what started this mess - going back isn't an option) that is tied to a DataSet that comes from a Web Service. When a user types in the item they want manually, the data from the text field doesn't seem to associate itself with the DisplayMember, which forces the WS/SQL query to fail (it is sent a blank value when it's expecting a ValueMember). If the user types in a partial selection and then chooses the value they want from the DisplayMember list using the arrow keys or tab, the query goes off without a problem.
My Question: How do I get the text field to translate to the DisplayMember which will then properly tie itself to the ValueMember which will then allow the query to execute correctly? Sorry for making this sound complicated or convoluted; I'm sure the answer is easy and I'm just glazing over it.
The relevant bit of code is:
With cmbDID
If dtsLU.Tables.Contains(reqTable) = True Then
.DataSource = dtsLU.Tables(reqTable)
.DisplayMember = "zip"
.ValueMember = "gridID"
End If
End With
cmbDID.DataBindings.Clear()
cmbDID.DataBindings.Add("SelectedValue", dtsData, strDT & ".gridID")
I've tried changing "SelectedValue" to "Text", which almost works - but it directly translates to gridID and skips zip which ends up with an incorrect Web Service response since the zip and gridID field values are not synced (zip (DisplayMember) may be 5123 while gridID (ValueMember) may be 6047). I've tried changing "SelectedValue" to "SelectedIndex", and that got me no where.
Any help is greatly appreciated.
EDIT
To add some clarification to the process, the below pseudo code / description is roughly what happens. I could post the whole module, but I feel that would just muddy the whole question even more.
Private Sub A
FormAlpha is created with 1 ComboBox in the form of a DropDown
This DropDown is populated with a DataSet
DataBinding with a blank DataSet is added to the control to keep track of the users input
End Sub
lblSubmit_Click event is triggered on FormAlpha by the user after they have populated the DropDown with their data. lblSubmit_Click calls Private Sub Submit
Private Sub Submit
BindingContext(DropDown DataSet, tableName).EndCurrentEdit() is called
DataSet.HasChanges() is processed
If changes are present, changes are processed
HERE lies the problem
If the user has manually typed in the DropDown field, but not hit an arrow key or tab, then the DataSet registers a change, but returns a null value in all fields - it knows something was entered, but that data apparently didn't pass through the DataSet for the ComboBox (ListItems or SelectedIndex didn't change / fire I'm guessing). If the user selects the item with the arrow keys, the DataSet has the proper input (I'm assuming the Data was validated by the control at this point).
If the processed data is good, a value is entered into the database
If the processed data is bad (empty), an error is returned
End Sub
If the above can't be solved with what I've provided, but someone still knows a better way to handle this type of situation, I'm all ears. Rewriting the module isn't ideal, but fixing this problem is a necessity.
Alright, while this fix may not be ideal, it is a fix none the less.
The bare bones problem was that the text value of the DropDown wasn't causing the data to actually affect the SelectedIndex / SelectedValue of the control unless you interacted with it using the arrow keys or a mouse click. So, while the DropDown would read "1234", in reality the control saw "".
The fix I have in place for this is simply calling comboBox.text = comboBox.text whenever the user hits the submit button.

Assigning DataGridView_1 (DGV) to DGV_2 and DGV_2 doesn't update

I have successfully found an answer to every question I ever had since I started with VB.NET 2 years ago by trawling forums, but this time I failed, and I decided to join my favourite forum where I have always found the best answers. This one :-)
I am somewhat of a beginner, so load you flack-guns 'cause here we go...
In my main form I have a DGV (called "gridDisplay") to display data. This DGV is a read only one as it is only used to disply data, not to inteact with it.
I have any number copies of a class (called "TaskData") that holds data to be displayed, and the data to be shown in the main form is that of the active "TaskData" class.
I came up with the brilliant (I have my doubts now...) idea to let the TaskData class make a DGV as it knows what data is in it and how to display it, and then all I had to do in the main form was to set the DGV there to that of the Active TaskData Class (see code below)
With ActiveTask
'Assign the active DataDisplay to the one in the main form
Me.gridDisplay = .TaskData.DataDisplay
Me.gridDisplay.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
Me.gridDisplay.Refresh() 'Trying to make it update
Me.gridDisplay.Update() 'Trying to make it update
MsgBox("Row count: " & Me.gridDisplay.RowCount)
End With
Ok, so the DGV in the .TaskData.DataDisplay has one column and 500 rows. The One in the main form is set up with a default of 2 columns and no rows (set up in the designer)
After the code above, nothing happens visually in the main form. Still 2 columns and 0 rows. However, the text box says: "Row count: 500" And if I put a break point there I can inspect Me.gridDisplay and it has all the data that should be there.
Why doesn't it show up?
Am I doing something unwise by assigning one DGV to another?
Any help would be much appreciated.
Any constructive critizism equally so :-)
I think you should call the datasource again before
Me.gridDisplay.Refresh()Me.gridDisplay.Update()
Something like this,
gridDisplay.DataSource = dataset.Tables["your table"];

When adding a checkbox to a datagridview datatable I get dataerrors

I have a datagridview that I've added a checkbox column to. When I preview my data without the checkbox it works fine. when I run the program and try to load the datatable, I get a data error. I'm not sure what may be causing this. Where can I look to figure out what's causing this error?
Thank you
Doug
Your e.Context returns as Display. If you take a look at MSDN you will see for display it says
A data error occurred when displaying
a cell that was populated by a data
source. This value indicates that the
value from the data source cannot be
displayed by the cell, or a mapping
that translates the value from the
data source to the cell is missing.
In other words the checkbox column in the dgv probably doesn't quite know how to display the information you are giving it. So, chances are you are telling the checkboxes whether they should be true or false incorrectly.
Update: Perhaps, your solution lies with the TrueValue and FalseValue properties of DataGridViewCheckBoxColumn Class. These properties define when a checkbox should be checked or not checked. If you don't have these set, or if you are trying to set your checkboxes to something other than either of the two values in these properties, you would have problems.

Grids get empty when I click on some other tab

I have a question, I have a combo box, which when changed fills the data grid. Now Wen I change the Tab and come back to same tab again(one containing the combo box)..the value of combo box remains there, however the grid gets empty. I need this data to be maintained till the user selects another value from drop down.How can this be done???
Any suggestions would be appreciated.
Some of the possible reasons :
There is a MyDataGrid.clear somewhere
The DataSource used to fill the grid is somewhere cleared.
If you use a DataSet, your DataTable is somewhere truncated.
If you use a BindingSource as your DataSource, maybe somewhere else you're using the same BindingSource, which empties the grid.
I would recommend you to put a braekpoint on your Combobox change value event to see if it's not called with a wrong value somewhere else.
And I would add a spy to check your DataGrid (the number of lines for example) to precisely know where it happens by executing my code step by step.
Hope this helps !

Best way to add extra values to a WinForms combo box based on a hashtable

This might be a bit of a dumb question, but I'm trying to add some extra key/value pairs to a combo box using VB.NET. The initial item list is generated from a hashtable, which contains a collection of objects.
I've managed to add the extra values to the box using the Add method, however I now run into problems when reading back the selected item from the combo box because some list items are objects, while others are strings.
My best option seems to be to load the initial data as key/value pairs by looping through the hashtable, however this doesn't seem to be working too well either because I'm still getting errors.
I'm getting frustrated because it's taking me hours to do something that should take 5 minutes!
I'll post some sample code if it will help.
You could check the type of SelectedItem on the combo box, and use that to determine whether you are dealing with one of your objects or not.
If TypeOf myComboBox.SelectedItem Is GetType(ObjectClass) Then
Else
End If