none
Settings.Settings Value for DataTable RRS feed

  • Question

  • private void Form1_Load(object sender, EventArgs e)

    { //other stuff dt = Properties.Settings.Default.datagrid; dataGridView1.DataSource = dt; }

    As seen above i have a windows form where a want a datagridview to populate when the form loads.

    The datagridview does not populate on start up. The properly save the DataTable to settings.

    My question is in settings I do not have a value for my dataTable. Is this the reason it does not work?

    And what can I put in value? Right now it is not accepting anything (even dt), with a message "cannot convert to system.data.datatable"

    Wednesday, November 8, 2017 4:50 PM

All replies

  • Why would you even want this?  There are better ways to load data if you are not using a database e.g. read from xml and write to xml which can produce a proper data set using DataSet.ReadXml and DataSet.WriteXml along with DataSet method for writing and reading an .xsd file to ensure proper data types are mapped to your data.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, November 8, 2017 5:57 PM
    Moderator
  • Hello Clutch_King,

    How do you save the DataTable to settings?  If you just want to use application settings to save datatable object, first you should set the  type of store value from application settings.

    Second run the below code(I created a datatable object for example)

     DataTable dt = new DataTable("my");
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("Marks") });
                dt.Rows.Add("Ravi", "500");
                Properties.Settings.Default.datagrid = dt;
                Properties.Settings.Default.Save();
    
    And you will see the store value under the below folder
    C:\Users\%YouradminName% \AppData\Local\%your project name%\WindowsFormsApp4.exe_Url_xtjmeyjg0u0jfd4aspdhodq01tlpbzlz\1.0.0.0\user.config

    Finally you could access value by below code.

     DataTable dt1 = Properties.Settings.Default.datagrid;
                dataGridView1.DataSource = dt1;
    The all above code works well under the environment of the windows10,vs2017,framework 4.6.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 9, 2017 10:24 AM
    Moderator
  • You don't show what "Properties.Settings.Default.datagrid" is. We must at least know that much. Is it a string or something else? What does the "Type" column have for the setting?

    As others have suggested, storing a table as a setting is not a good solution usually. However for small amounts of data if it is your preference then there is not a big problem technically. Many other programmers would say it is not good but it is more of a subjective question than objective.

    Without knowing what the data type of datagrid is, I suggest you store the data as tab-delimited data. Then convert the data to/rom that. Create a collection of some type, such as a List<dataobjects> where dataobjects is a class for the data. Set the dataGridView1.DataSource to the instance of List<dataobjects>.

    I have stored data in a setting as delimited data like that but in that situation the data had "Application" scope; it was read-only for the application.



    Sam Hobbs
    SimpleSamples.Info


    Thursday, November 9, 2017 8:28 PM
  • Hello ,

    Is there anything updated? If the issues has been solved, you could close the thread by marking as answer, This can be beneficial to other community members reading this thread.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 10:33 AM
    Moderator