i have trouble to save DataGridView1,i dont know how to declare it

Racine Mackosso
Racine Mackosso used Ask the Experts™
on
Imports System.Data.DataTable
Public Class Form3
    Dim table As New DataTable("Table")
    Dim index As Integer
    Dim DataGridView1 As String

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        table.Columns.Add("Nom", Type.GetType("System.String"))
        table.Columns.Add("Prenom", Type.GetType("System.String"))
        table.Columns.Add("Date de naissance", Type.GetType("System.String"))
        table.Columns.Add("Domicile", Type.GetType("System.String"))
        table.Columns.Add("Numero de telephone", Type.GetType("System.String"))
        DataGridView1.DataSource = table
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        table.Rows.Add(TextBoxN.Text, TextBoxP.Text, TextBoxDdn.Text, TextBoxD.Text, TextBoxNdt.Text)
        DataGridView1.DataSource = table
    End Sub

    Private Sub btnsuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuivant.Click
        Form4.Show()
    End Sub

    Private Sub btneffacer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneffacer.Click
        DataGridView1.Rows.RemoveAt(index)
    End Sub

    Private Sub Btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        'clear TextBoxes
        TextBoxN.Text = ""
        TextBoxP.Text = ""
        TextBoxDdn.Text = ""
        TextBoxD.Text = ""
        TextBoxNdt.Text = ""
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        'save TextBoxes
        Dim isave As New SaveFileDialog
        isave.Filter = "txt files (*.txt) |*.txt"
        isave.FilterIndex = 2
        isave.RestoreDirectory = False

        If isave.ShowDialog() = DialogResult.OK Then
            IO.File.WriteAllText(isave.FileName, DataGridView1)
        End If


    End Sub
End Class
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Distinguished Expert 2017
Commented:
Hi Racine;
 
You have defined the DatagridView as a string as shown here
Dim DataGridView1 As String

Open in new window

where it should be defined as follows
Dim DataGridView1 As New DataGridView()

Open in new window

Correct that to see if that helps.
NorieAnalyst Assistant

Commented:
Is the datagridview on a form?

If it is then I don't think you need to declare it.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
From the information given I have posted the solution that is causing the issue.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial