Вопрос

Hi I'm new to programming but have learned pretty fast and have most of my small application figured out. But the part that I'm working on now has got me. I have a form with some text boxes that after they are filled out get added as a new row to my datagridview. But some of the textboxes are joined together such as boxes for First, Middle, and Last name, which are added to one cell in the format of Last, First Middle, because the .csv that will be exported is used for dataset variables in Photoshop and there is one single text line for the full name. I have some code using the stringsplit, and it actually works, except it always loads the cell data from the first row, even when I click on a different row and it adds the data that isnt using the stringsplit. Heres part of the code:

Dim i As Integer
Dim line As String = DataGridView1.Item(1, i).Value
Dim separators() As String = {",", " "}
Dim name() As String
i = DataGridView1.CurrentRow.Index
name = line.Split(separators, StringSplitOptions.RemoveEmptyEntries)
txtOrder.Text = DataGridView1.Item(0, i).Value
txtFirst.Text = name(1)
txtMiddle.Text = name(2)
txtLast.Text = name(0)
txtAddress.Text = DataGridView1.Item(2, i).Value

So say I have two rows in my datagridview, each represent a different order with different data. When I click on the second row, it loads the correct values for the txtOrder.text and txtAddress.text, but it loads the first, middle, and last names from the FIRST row to the three text boxes (albeit it correctly splits string and adds it correctly to the textboxes).

I hope I explained the problem enough, this is my first post ever asking about programming lol. Hopefully someone can help me out! Thanks Taylor

Edit: So of course after all the searching I did it was an easy fix. Heres the modified code, moved "i=datagridview1.currentrow.index" to right below "Dim i as Integer". Part of the learning process I guess! lol.

Dim i As Integer
i = DataGridView1.CurrentRow.Index

Dim line As String = DataGridView1.Item(1, i).Value
Dim separators() As String = {",", " "}
Dim name() As String

name = line.Split(separators, StringSplitOptions.RemoveEmptyEntries)

txtOrder.Text = DataGridView1.Item(0, i).Value
txtFirst.Text = name(1)
txtMiddle.Text = name(2)
txtLast.Text = name(0)
txtAddress.Text = DataGridView1.Item(2, i).Value
Это было полезно?

Решение

we have no context for the code such as whether it is a click event etc, and your question is hard to follow, but this seems to be wrong:

Dim i As Integer
Dim line As String = DataGridView1.Item(1, i).Value

i will always be zero - you just declared it on the previous line, so it will always set line to the first row (row (0))

i = DataGridView1.CurrentRow.Index

This might not be what you want either depending where this is, if it is a loop etc.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top