Question Get data from DataGrid to Textbox

Revaliz

Member
Joined
Nov 11, 2012
Messages
6
Programming Experience
Beginner
I was wondering how you would make a code so that what I write in a Datagrid would come out on a textbox with help of a button.
Basicly I write 250 in the Datagrid, and when I press the button, 250 comes out on the Textbox wich is on the same form. I have been trying to find out how to do this, but can't find it anywere... :( Thanks for any answers :)
 
Assuming that what you're actually using is a Windows Forms DataGridView, the short answer is that you get the appropriate cell from the grid and then get its Value property and then display that in the TextBox the same way you would any other data.

You want to provide a more precise description of what you're trying to do though. The whole point of a grid is that it will display multiple rows and columns so presumably you don't just have one value in it. What are you actually trying to achieve?
 
I am trying to make a program wich does display a coded form of the text in the grid. If i write 250|400 in one columns in the grid, i want the textbox to show [village]250|400[/village] or in coded form "[village]" + vg1 + "[/village]"

If the code for colone 1, line 1 is vg1
 
Hi,

I am not really sure what you are trying to do here and you have not confirmed that you are using a DataGridView but aside from that here is just one way to access a selected DataGridView cell and add it to a TextBox the way you want.

VB.NET:
If Not IsNothing(DataGridView1.SelectedCells(0).Value) Then
  TextBox1.Text = "[village]" & DataGridView1.SelectedCells(0).Value.ToString & "[/village]"
End If
Hope that helps.

Cheers,

Ian
 
What kind of tool are you looking to build Revaliz?
A support tool that converts the content to a clear overview in a table :)

And yes I am using DataGridView
Thanks for the code :) But this would only work for 1 selected
column. This is what I am planning, so sorry for norwegian text :p
Tribalwars.jpg

But I can't get row 1, line 1 to go to one place and row 1, line 2 to go to another place
If row 1, line 1 is S1. Line 2 is s2


Like this: "[village]" + s1 + "[/village] | [village]" + s2 + "[/village]"

But then there is another problem
If row 1, line 1 is S1. Line 2 is s2
and row 3, line 1 is r1, Line 2 is r2

Then I want to insert the code like this:
r1 + "|" + "[village]" + s1+ "[/village] | Time of arrival:" + r1 + "| [village]" + s2 + "[/village] |"

And btw you press the update button to get the coded format

Hope you understood what i meant :)
 
Last edited:
Ok, so when you type in a village reference in the "Angriperen's" (Destination) column, you want the textbox to display something like:

"Send Support to [village]" & Angriperen's Village Reference & "[/village]"

So that you can just copy the text from the textbox and paste it to the tribe's forum yes?


I assume then that you also want to include the other data in that report such as number of Spears, Number of Swords, Number of Axes etc?


In that case, you just want to "For Each" your way through the grid and build a string as you do so..

I have just been called away from the computer so I cannot show you code examples right now but basically you need to loop through each row, checking if the Destination Village Cell's .value is not blank.. If the value is not blank then you need to loop through each cell in the row, taking the value from the cell, building your string just as Ian Ryder showed you in his example above.

Good Luck
 
Thanks :) And yes, that is basicly what I am trying ot do, but can you show me an example of the code? :p Because I am not so shure of what you meant :p
 
Ok i'm back, just so i understand you correctly because you updated your post while I was typing my reply earlier so I didnt see it, and you are saying row when you actually mean column so it made it more confusing.. Row = horizontal (left to right)... Column = vertical (top to bottom)

You want the message generated to use data from multiple rows? Will there always be only 2 rows of data? Or will there be sometimes 3 or 4 or 5 rows?
 
Yes 22-degreesm there will sometimes be 3,4 or 5 rows. Depends how many rows the user want to make. Because if the user gets more attacks, he will want to add 1 row for each attack. So the user can add 150 rows if he gets 150 attacks and so on.
 
Ah ok, it is now beginning to make sense.. I mis-translated the word "Angriperen" and thought you were making a "Support Order" tool rather than a "Support Request" tool.. I will draw something up for you and send it to you later today..
 
Ok here you go, you will have to change the names of the columns in the datagrid to reflect the names I have used in the code.. The names are very similar to your Column Header Text. And you can translate the English bit to Norwegian and add any filler to improve the context yourself. I replicated what you showed in the example above and added the extra data for individual troop-number request.


   Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click

Dim msgString As String, angripCnt As Integer

      For rowCnt = 0 To DataGridView1.Rows.Count - 1

         If DataGridView1.Rows(rowCnt).Cells("angriperenslandsby").Value <> Nothing Then

            msgString &= DataGridView1.Rows(rowCnt).Cells("Ankmost").Value & " | [village]" & DataGridView1.Rows(rowCnt).Cells("dinLandsby").Value _
               & "[/village] | Time of arrival: " & DataGridView1.Rows(rowCnt).Cells("Ankmost").Value & " | [village]" _
               & DataGridView1.Rows(rowCnt).Cells("angriperensLandsby").Value & "[/village]" & vbNewLine

            If DataGridView1.Rows(rowCnt).Cells("spyd").Value <> Nothing Then
               msgString &= "I need " & DataGridView1.Rows(rowCnt).Cells("spyd").Value & " Spears" & vbNewLine
            End If

            If DataGridView1.Rows(rowCnt).Cells("sverdmenn").Value <> Nothing Then
               msgString &= "I need " & DataGridView1.Rows(rowCnt).Cells("sverdmenn").Value & " Swords" & vbNewLine
            End If

            If DataGridView1.Rows(rowCnt).Cells("oksmenn").Value <> Nothing Then
               msgString &= "I need " & DataGridView1.Rows(rowCnt).Cells("oksmenn").Value & " Axes" & vbNewLine
            End If

            If DataGridView1.Rows(rowCnt).Cells("bueskytter").Value <> Nothing Then
               msgString &= "I need " & DataGridView1.Rows(rowCnt).Cells("bueskytter").Value & " Archers" & vbNewLine
            End If

            msgString &= vbNewLine


            angripCnt += 1

         End If

      Next


      RichTextBox1.Text = "Support Request - Incoming Attacks x " & angripCnt & vbNewLine & vbNewLine & msgString


   End Sub
 
Back
Top