DataGridView speichern/laden

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 37 Antworten in diesem Thema. Der letzte Beitrag () ist von Jason.

    DataGridView speichern/laden

    Hallo.

    Ich habe mir vorhin ein Video über das Speichern der Daten einer DataGridView angsehen.

    Hier mein bisheriger Code:

    VB.NET-Quellcode

    1. Private dt_DATA As New DataTable
    2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    3. With dt_DATA
    4. .TableName = "Adressen"
    5. .Columns.Add("Telefon")
    6. .Columns.Add("E-Mail")
    7. End With
    8. DataGridView1.DataSource = dt_DATA
    9. End Sub
    10. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    11. Dim sw As New System.IO.StringWriter()
    12. dt_DATA.WriteXml(sw)
    13. End Sub

    Wenn ich auf den Button klicke, passiert nichts.
    Habe ich etwas vergessen?
    Einen Pfad vllt ?

    EDIT: Ich würde außerdem nicht nen StringWriter sondern nur einen Stream nehmen.
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Naja, ist ja auch klar, dein "sw" ist ja auch noch "new" ohne werte. Du musst schon sagen wo er etwas speichern soll
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    Lösung steht und Post 2 und 3
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    Ich kenne das Video und das Programm soll den XML-Inhalt des DGVs in einen String schreiben.
    Der Author sagte aber auch: Inwiefern ihr diesen String dann weiterverwendet, bleibt euch überlassen.
    (FTP-Upload, Datei schreiben, etc.)

    Wenn schon bitte richtig gucken ;)!
    Polling is trolling!

    Achtung: Ich habe die komische Angewohnheit, simple Dinge zu verkomplizieren..

    VB.NET-Quellcode

    1. Dim sw As New System.IO.StringWriter("data.xml")
    2. dt_DATA.WriteXml(sw)​

    Wenn ich den Pfad angebe bekomme ich eine Fehlermeldung:

    Quellcode

    1. ​Das Objekt des Typs "System.String" kann nicht in Typ "System.IFormatProvider" umgewandelt werden.
    Schau Dir halt die Konstruktoren und Methoden auf MSDN an, dann siehst Du, was Du angeben musst und kannst passende Überladungen raussuchen.
    Nicht böse gemeint, aber zeig' doch bitte mal etwas Eigeninitiative, man kann Dir nicht immer alles hinterhertragen. Ich bin mir sicher, dass Du somit auch viele Probleme eigenständig lösen könntest. ;)

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Sehe ich auch so, ist auch von mir nicht böse gemeint.
    Aber wenn man alle Werkzeuge + Erklärung mit Ton erhält, dann schon richtig schauen und zumindest zuhören.

    PS: MSDN
    Polling is trolling!

    Achtung: Ich habe die komische Angewohnheit, simple Dinge zu verkomplizieren..
    Ja ok ^^
    Meistens suche ich auch mal selber.
    Hier im Forum wird man IMMER Super geholfen! :thumbsup:

    ---

    VB.NET-Quellcode

    1. Public Sub WriteXml(_fileName As String)
    2. Dim DS As DataSet = dt_DATA
    3. DS.WriteXml(_fileName)
    4. End Sub​

    Ich habe es so versucht, ist aber nicht richtig.
    Ist das denn schon der richtige Ansatz?
    Nein
    Ein DataSet ist kein DataTable

    Versuch mal so was:

    VB.NET-Quellcode

    1. Dim Pfad As String = "Y:\XMLTest.xml"
    2. dt_DATA.WriteXml(Pfad)
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    Die XML Datei wird erstellt.
    Aber die eingegebenen Werte sind nicht in der Datei vorhanden:

    XML-Quellcode

    1. <?xml version="1.0" standalone="yes"?>
    2. <DocumentElement>
    3. <Adressen />
    4. <Adressen />
    5. </DocumentElement>



    ---

    Wenn ich dem DataTable den Inhalt der DGV zuweise, wird auch nur der TableName in die Datei geschrieben.

    VB.NET-Quellcode

    1. Dim Pfad As String = "Data.xml"
    2. dt_DATA = DataGridView1.DataSource
    3. dt_DATA.WriteXml(Pfad)

    Hier bekomme ich einen Fehler: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

    VB.NET-Quellcode

    1. dt_DATA.WriteXml("Data.xml")​


    VB.NET-Quellcode

    1. ​dt_DATA = CType(DataGridView1.DataSource, DataTable)
    2. dt_DATA.WriteXml("Data.xml")

    Jason schrieb:

    dt_DATA = CType(DataGridView1.DataSource, DataTable)


    warum machst du das?
    dt_DATA ist doch die Datasource von DataGridView1. Das Umdrehen mach doch gar keinen Sinn.

    "Data.xml" ist kein Pfad "C:\Data.xml" ist ein Pfad
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    Als ich Option Strict auf On gestellt habe, musste ich das ändern (Kontextmanu-Fehlerkorrektur).
    Das vorherige wurde dann als Fehler angezeigt.
    Seltsam...

    ---

    Ich kann Data.xml angeben, dann ist die Datei im Programmpfad.

    Das wird irgendwie automatisch ergänzt.
    Klar weil Strict dir sagt das eine DataSource keine DataTable ist. Auf die Weise sagst du deinem Programm das es versuchen soll aus der DataSource eine DataTable zu machen. Ob Das funktioniert ist der Syntax Prüfung egal.

    VB.NET-Quellcode

    1. Dim i As Integer = Convert.ToInt32("A")


    Wird mir auch nicht als "Falsch" markiert aber wenn ich es Laufen lasse bekomme ich dennoch einen Fehler.
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."