none
how can i transfer data from datagridview to textbox RRS feed

  • Question

  • Dear All 

    i need something similar to attached image. 1st row of datagridview must fill with all details of textbox but in 2nd row and onward i do not want to fill the column2 section . i tried below code but it making extra row  with all information .

      Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
    
    
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
    
    
                For ia As Integer = 0 To DataGridView1.Rows.Count - 1
    
                    If DataGridView1.Rows(ia).Cells(1).Value > 0 Then
    
                        DataGridView1.Rows.Add(TextBox1.Text, "0", TextBox3.Text, TextBox4.Text)
    
                    Else
    
                        DataGridView1.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
    
    
                        '    DataGridView1.Rows(ia).Cells(8).Style.BackColor = Color.Red
    
                    End If
    
                Next
    
    
            End If
    
    
    
    
        End Sub

    i need to fill row 2 and onward with textboxes

    Saturday, July 6, 2019 6:04 AM

Answers

  • Hi

    You say 'i do not want to fill the column2 section' but then you use a value of 0 - do you want zero in Column2 after row 0? or, blank cell?

    Here is a version with blank cells after row 0.

    Where does the TextBox enter into the story?

    	Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox4.KeyPress
    		If e.KeyChar = ChrW(Keys.Return) Then
    			If DataGridView1.Rows.Count > 1 Then
    				DataGridView1.Rows.Add(TextBox1.Text, Nothing, TextBox3.Text, TextBox4.Text)
    			Else
    				DataGridView1.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
    			End If
    		End If
    	End Sub


    Regards Les, Livingston, Scotland


    • Marked as answer by Omi4u Saturday, July 6, 2019 6:37 AM
    • Edited by leshay Saturday, July 6, 2019 6:38 AM
    Saturday, July 6, 2019 6:35 AM
  • thank you for the answer but is it possible that i can make something like

    if datagridview.row.column("column2") > 1 then

    i mean can we use a specific column name 


    Hi

    No! you can't do that. Did you not see the Intellisense descriptions that popped up?

    I *think* you are maybe wanting something like this, but note the rows index needed there.

    If CInt(DataGridView1.Rows(11).Cells("Column2").Value) > 1 Then
    
    	' do something
    
    End If
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Omi4u Saturday, July 6, 2019 7:30 AM
    • Unmarked as answer by Omi4u Saturday, July 6, 2019 7:30 AM
    • Marked as answer by Omi4u Saturday, July 6, 2019 8:03 AM
    Saturday, July 6, 2019 6:59 AM

All replies

  • Hi

    You say 'i do not want to fill the column2 section' but then you use a value of 0 - do you want zero in Column2 after row 0? or, blank cell?

    Here is a version with blank cells after row 0.

    Where does the TextBox enter into the story?

    	Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox4.KeyPress
    		If e.KeyChar = ChrW(Keys.Return) Then
    			If DataGridView1.Rows.Count > 1 Then
    				DataGridView1.Rows.Add(TextBox1.Text, Nothing, TextBox3.Text, TextBox4.Text)
    			Else
    				DataGridView1.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
    			End If
    		End If
    	End Sub


    Regards Les, Livingston, Scotland


    • Marked as answer by Omi4u Saturday, July 6, 2019 6:37 AM
    • Edited by leshay Saturday, July 6, 2019 6:38 AM
    Saturday, July 6, 2019 6:35 AM
  • Thanks man it worked 
    Saturday, July 6, 2019 6:37 AM
  • thank you for the answer but is it possible that i can make something like

    if datagridview.row.column("column2") > 1 then

    i mean can we use a specific column name 


    • Edited by Omi4u Saturday, July 6, 2019 6:43 AM
    Saturday, July 6, 2019 6:43 AM
  • thank you for the answer but is it possible that i can make something like

    if datagridview.row.column("column2") > 1 then

    i mean can we use a specific column name 


    Hi

    No! you can't do that. Did you not see the Intellisense descriptions that popped up?

    I *think* you are maybe wanting something like this, but note the rows index needed there.

    If CInt(DataGridView1.Rows(11).Cells("Column2").Value) > 1 Then
    
    	' do something
    
    End If
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Omi4u Saturday, July 6, 2019 7:30 AM
    • Unmarked as answer by Omi4u Saturday, July 6, 2019 7:30 AM
    • Marked as answer by Omi4u Saturday, July 6, 2019 8:03 AM
    Saturday, July 6, 2019 6:59 AM
  • thanks ok my issue is resolved thank you 
    Saturday, July 6, 2019 7:30 AM