Add data to DataGridView without MySQL? Possible?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 935 Views - Last Post: 09 August 2016 - 07:11 AM Rate Topic: -----

#1 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 01:36 AM

I am currently working on a small project, its a password management application(personal use only)

And im novice when it comes to DataGridView and I was wondering if its possible to add new columns with data without having to hardcode it into the software.

For example.. If I start the application I create a new column, then input some data to the columns. And when I restart the application the data is still there.

Thanks.

This post has been edited by DOT1: 09 August 2016 - 01:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Add data to DataGridView without MySQL? Possible?

#2 JapanDave   User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 390
  • Joined: 01-February 16

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 02:31 AM

Where do you plan to store the data after it is input? Also adding columns programatically is much more difficult than adding rows.

What is this WniForms or WPF?

In any case , I can't see how you are going to accomplish this without a database.
Was This Post Helpful? 0
  • +
  • -

#3 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 02:50 AM

I was thinking that I could maybe create a textfile to store all the info in but I guess that would be too much hard work for such a simple task..
Its a WinForm application..

And my bad, im only adding rows with columns already created.
Was This Post Helpful? 0
  • +
  • -

#4 JapanDave   User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 390
  • Joined: 01-February 16

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 03:12 AM

A textfile is a very limited medium. You can append data the the end of the data block, but not to the middle. So you would have to overwrite the complete file data block each time to update the data, not a very nice way to do something like this.

What exactly are you trying do and with what kind of data and how much?
Was This Post Helpful? 0
  • +
  • -

#5 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 03:13 AM

Basically I have this application its just a datagridview and I want to be able to add data to it and when I restart the application I want the data to still be there.
If that makes any sense.
Was This Post Helpful? 0
  • +
  • -

#6 JapanDave   User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 390
  • Joined: 01-February 16

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 03:24 AM

Yeah, that makes sense.

You could add the data from the textfile to the datagrid by looping through all the data (making sure you have a delimiter to separate your columns). The issue would be that you would then have to get that data you have input, say before close and overwrite the complete textfile data with the new data.
Was This Post Helpful? 0
  • +
  • -

#7 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 03:39 AM

Hm.. Well, seems like it would be easier with mySQL, Its just that I dont want to get SQLInjected.
(New to mySQL)

What would my options bw for something like this? Like creating a password manager application?

Maybe something that prints out textfiles with encrypted text and when I open the textfile in my application it decrypts it? I dont really new, testing things out to learn :P
Was This Post Helpful? 0
  • +
  • -

#8 JapanDave   User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 390
  • Joined: 01-February 16

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 03:49 AM

Who is going to do that to you if you are the only one using the database. But, in any case using parameters in correctly constructed SQL queries make it extremely difficult to do SQL injection and is something I really don't think you need to worry about. Maybe I am going out on a limb and saying your data in the table that can be stored in a textfile, I am pretty sure it would not be that difficult to re-populate the database with data even if a table was dropped(Again, I would like to stress how that could happen if it is just you using the program)

I honestly would use something that is designed to be the back-end (database) of an interface like WinForms. It would be far easier.

This post has been edited by JapanDave: 09 August 2016 - 03:53 AM

Was This Post Helpful? 0
  • +
  • -

#9 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 04:30 AM

Alright, I'l hook it up to a back end service such as mySQL, and I'l look into security after that, Might be a good tool to have (Database security).

Thanks! :D
Was This Post Helpful? 0
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7250
  • View blog
  • Posts: 15,123
  • Joined: 16-October 07

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 05:07 AM

Hmm... This is probably easier than you think.

You want to simply persist data? The magic word for that is serialization.

You want to use a dataview? Well, the easiest way to use a dataview is to bind it to a datatable. And, well, datatables can be stored and retrieved very easily.

Quick proof of concept:
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
  public static class Repo {
    const string FILENAME = "data.xml";
    private static DataSet ds = null;
    // call if file doesn't exist
    private static DataTable InitDefault() {
      var dt = new DataTable();
      dt.Columns.Add("Name", typeof(string));
      dt.Columns.Add("Age", typeof(int));
      return dt;
    }

    public static DataTable Data {
      get {
        if (ds == null) {
          ds = new DataSet();
          if (System.IO.File.Exists(FILENAME)) {
            ds.ReadXml(FILENAME);
          } else {
            ds.Tables.Add(InitDefault());
          }
        }
        return ds.Tables[0];
      }
    }
    public static void Save() {
      Data.WriteXml(FILENAME);
    }
  }

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      dataGridView1.DataSource = Repo.Data;
      this.FormClosing += (x, e) => { Repo.Save(); };
    }
  }
}



So, my Repo offers data. If that data isn't loaded yet, it reads from a file or creates an empty dataset. The save just saves the data object again. That's pretty much it for the form, bind data to view, save on close.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#11 JapanDave   User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 390
  • Joined: 01-February 16

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 05:18 AM

So you are saving it all in the XML? Did not know you could do that do. Learnt something new, thanks Baavgai.
Was This Post Helpful? 0
  • +
  • -

#12 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 05:24 AM

Oh that works aswell, but wont I have to call a streamwriter to create the xml?
Was This Post Helpful? 0
  • +
  • -

#13 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 05:31 AM

Also.. Is there a way to encrypt the xml file? Because lets say i generate a xml file and use that as a database and load it back in as soon as the pplication start.. Thats fine and all but wouldnt it make it really easy to read all the data thats inside the xml?
Was This Post Helpful? 0
  • +
  • -

#14 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7250
  • View blog
  • Posts: 15,123
  • Joined: 16-October 07

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 05:34 AM

View PostDOT1, on 09 August 2016 - 07:24 AM, said:

call a streamwriter to create the xml?


You could if you want. You'd need to if it wasn't a file. However, DataSet has numerous overloads for WriteXml, including just a file name.

Note, the XML thing has those nice helper methods and is thus exceptionally expedient. However, DataSet supports ISerializable so could be stored as pretty much anything.
Was This Post Helpful? 0
  • +
  • -

#15 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Add data to DataGridView without MySQL? Possible?

Posted 09 August 2016 - 05:35 AM

Ooh! Thats pretty nit!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2