Click here to Skip to main content
15,902,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm a programming noob and I'm having trouble w/ the DataGridView. The grid on my form has 3 columns (Position | Length | Type). I also have 2 textboxes, 1 for "POSITION", 1 for "LENGTH", a group of radio buttons for the "TYPE",and an "ADD" button on my form. I want to be able to fill in the textboxes, choose the type (radio button), then click the "ADD" button to add the data to the DataGridView in a new row -

Example:

new row = ( Position | Length | Type )

CODE-->

VB
Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
'Add new row
DataGridView.NewRow
'Add data to the corresponding cells
DataGridView.NewCell(0,0) = txtPosition.Text
DataGridView.NewCell(0,0) = txtLength.Text
If radV.checked = True Then
DataGridView.NewCell(0,0) = "V"
ElseIf radH.checked = True Then
DataGridView.NewCell(0,0) = "H"
End If
End Sub


Then I need to be able to save the entire data from the grid to a notepad.txt file w/ the same line/row structure w/o the column headers. If anyone can help, I would greatly appreciate it!
Posted

Hope it will help you:
Dim sb As New System.Text.StringBuilder
        'output column names
        sb.AppendLine(String.Format("{0};{1};{2}", Me.DataGridView.Columns(0).Name.ToString, Me.DataGridView.Columns(1).Name.ToString, Me.DataGridView.Columns(2).Name.ToString))
        ' output values
        For Each dr As DataGridViewRow In Me.DataGridView.Rows
            If dr.Cells(0).Value IsNot Nothing AndAlso dr.Cells(1).Value IsNot Nothing AndAlso dr.Cells(2).Value IsNot Nothing Then
                sb.AppendLine(String.Format("{0};{1};{2}", dr.Cells(0).Value.ToString, dr.Cells(1).Value.ToString, dr.Cells(2).Value.ToString))
            End If
        Next
        My.Computer.FileSystem.WriteAllText("C:\Test.txt", sb.ToString, False)
 
Share this answer
 
Comments
7774tlas 13-Feb-12 18:02pm    
Thank You! This is the final step in a program that I've been working on for a while, and now I can finally use it. Your code really helps me understand the process as well, very nice. I figured the save file process would have been alot more complicated. Thanks again for sharing the knowledge!
Also You have change your above code as well

VB
Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click

        'Add new row

        DataGridView1.Rows.Add()

        'Add data to the corresponding cells
        DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(0).Value = txtPosition.Text
        DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(1).Value = txtLength.Text
        If radV.checked = True Then
            DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(2).Value = "V"
        ElseIf radH.checked = True Then
            DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(2).Value = "H"
        End If


End Sub
 
Share this answer
 
Comments
7774tlas 13-Feb-12 18:18pm    
The "DataGridView1.Rows.Add()" w/ the "(DataGridView1.NewRowIndex)" solved the issue of adding a new row in order. I was trying "DataGridView1.Rows.Insert()" (which wasn't working properly). Thanks to you and Raimis9 for keeping it simple and clean for novices like me. Your help and knowledge are greatly appreciated!
RDBurmon 14-Feb-12 1:11am    
Welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900