0

I have created a dataset with a datatable inside of it, and have successfully got it to load to my datagridview(I believe, all empty values but I believe it is actually passing the table to the grid). issue is that the table appears to be completely empty when it gets to my gridview. all of the correct columns are there, and the few columns I have added values too have been populated with "0". these are the columns I expected to be filled with data from the json file. Currently there is an error "type Datum does not contain a public instance or extension definition for getEnumerator". Something is wrong with how I have my foreach statement set up. but I'm not aware of how to do it properly. Seems my Datum Class isn't the proper way to hold the deserialize values? but all I have read online points to using this method.

At this point all I am hoping for is to get the data displayed to the gridview. I am newer to c#, but I have been patiently working on this one part of this program for over a week now.

I am very aware there are going to be numerous obvious issues with the code. I just want to see my data displayed and then I will start to fix the large multitude of issues, currently this will build and run.

Main code

 public void mainForm_Load(object sender, EventArgs e)
    {
        var loadscreen = new SplashScreen();
        loadscreen.Show();


        //json file holding all data to be parsed.
        string myDynamicJSON = File.ReadAllText(@"testLibrary.json");

        //the data
        Datum ToolData = JsonConvert.DeserializeObject<Datum>(myDynamicJSON);
        Geometry ToolGeometry = JsonConvert.DeserializeObject<Geometry>(myDynamicJSON);

        //DataTable with something in it, do the binding
        BindingSource SBind = new BindingSource();
        SBind.DataSource = tooldataSet.Tables["Tool"];
        


        //looks into File finds json fields, and assign them to variables to be used in C# to 
        create the rows.
        foreach (var item in ToolData)
        {
            string description = ToolData.Description;
            string vendor = ToolData.Vendor;
            double cost = ToolData.Cost;
            int serial = ToolData.ProductId;
            string employee = ToolData.employee;
            string location = ToolData.location;
            bool returntool = ToolData.returnTool;
            int onHand = ToolData.onHandQty;
            int stockQty = ToolData.stockQty;
            int orderQty = ToolData.orderQty;
            string toolType = ToolData.Type;

            //some values are in toolgeometry instead of tooldata
            double diameter = ToolGeometry.Dc;
            double OAL = ToolGeometry.Oal;

            //Populate the DataTable with rows of data
            DataRow dr = tooldataSet.Tool.NewRow();
            // Fill the values
            dr["Description"] = description;
            dr["Vendor"] = vendor;
            dr["Cost"] = cost;
            dr["Serial #"] = serial;
            dr["Employee"] = employee;
            dr["Location"] = location;
            dr["OnHand"] = onHand;
            dr["StockQty"] =stockQty;
            dr["OrderQty"] = orderQty;
            dr["Return"] = returntool;
            dr["Diameter"] = diameter;
            dr["OAL"] = OAL;
            dr["Type"] = toolType;


            //once all data is added to the row, add the row, and loop untill all data is 
            loaded.
            tooldataSet.Tool.Rows.Add(dr);
        }
        //bind our dataset.table to the gridview
        toolDataGridView.DataSource = SBind;

        //hide splashscreen once Gridview has loaded the data
        loadscreen.Hide();
    }

Datum Class

public partial class Datum
{
    [JsonProperty("BMC")]
    public Bmc Bmc { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("geometry")]
    public Geometry Geometry { get; set; }

    [JsonProperty("guid")]
    public Guid Guid { get; set; }

    [JsonProperty("post-process")]
    public PostProcess PostProcess { get; set; }

    [JsonProperty("product-id")]
    public int ProductId { get; set; }

    [JsonProperty("product-link")]
    public string ProductLink { get; set; }

    [JsonProperty("start-values")]
    public StartValues StartValues { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("unit")]
    public Unit Unit { get; set; }

    [JsonProperty("vendor")]
    public string Vendor { get; set; }

    [JsonProperty("Cost")]
    public double Cost { get; set; }

    [JsonProperty("Employee")]
    public string employee { get; set; }

    [JsonProperty("Location")]
    public string location { get; set; }

    [JsonProperty("onHandQty")]
    public int onHandQty { get; set; }

    [JsonProperty("stockQty")]
    public int stockQty { get; set; }

    [JsonProperty("orderQty")]
    public int orderQty { get; set; }

    [JsonProperty("ReturnTool")]
    public bool returnTool { get; set; }

}

Finally our Json Finally to deserialize

{
"data": [
    {
        "BMC": "carbide",
        "GRADE": "Mill Generic",
        "description": "5/8-11\"",
        "geometry": {
            "CSP": false,
            "DC": 0.433,
            "HAND": true,
            "LB": 2,
            "LCF": 0.5,
            "NOF": 4,
            "NT": 1,
            "OAL": 5,
            "SFDM": 0.625,
            "TP": 0.0909091,
            "shoulder-length": 1.969,
            "thread-profile-angle": 60
        },
        "guid": "0112c196-8a79-421d-8dda-d4aa964aa6d7",
        "holder": {
            "description": "Maritool CAT40-ER32-2.35",
            "guid": "e800051b-e2d6-4699-a2b6-dad6466a0a0c",
            "last_modified": 1485790626152,
            "product-id": "CAT40-ER32-2.35",
            "product-link": "",
            "segments": [
                {
                    "height": 0.148,
                    "lower-diameter": 1.5,
                    "upper-diameter": 1.97
                },
                {
                    "height": 0.836,
                    "lower-diameter": 1.97,
                    "upper-diameter": 1.97
                },
                {
                    "height": 0.176,
                    "lower-diameter": 1.57,
                    "upper-diameter": 1.57
                },
                {
                    "height": 0.09,
                    "lower-diameter": 1.57,
                    "upper-diameter": 1.75
                },
                {
                    "height": 0.425,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.75
                },
                {
                    "height": 0.05,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.85
                },
                {
                    "height": 0.03,
                    "lower-diameter": 2.442,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.145,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.079,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.215
                },
                {
                    "height": 0.118,
                    "lower-diameter": 2.215,
                    "upper-diameter": 2.215
                },
                {
                    "height": 0.079,
                    "lower-diameter": 2.215,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.143,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.03,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.442
                },
                {
                    "height": 0.125,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.75
                }
            ],
            "type": "holder",
            "unit": "inches",
            "vendor": "Maritool"
        },
        "post-process": {
            "break-control": false,
            "comment": "",
            "diameter-offset": 17,
            "length-offset": 17,
            "live": true,
            "manual-tool-change": false,
            "number": 17,
            "turret": 0
        },
        "product-id": "GMDTTM58-11UN4FL",
        "product-link": "6010",
        "start-values": {
            "presets": [
                {
                    "description": "",
                    "f_n": 0.012242786571039,
                    "f_z": 0.0031,
                    "guid": "eac33213-4083-35b8-afe3-17bd42a249f0",
                    "n": 4410.76054758139,
                    "n_ramp": 44110,
                    "name": "1018",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 499.9999999999994,
                    "v_f": 54.6934307900093,
                    "v_f_leadIn": 54,
                    "v_f_leadOut": 54,
                    "v_f_plunge": 54,
                    "v_f_ramp": 54
                },
                {
                    "description": "",
                    "f_n": 0.010769117817118,
                    "f_z": 0.0028,
                    "guid": "f35f5601-68a9-477b-b4b0-2ffeedb3bef1",
                    "n": 1764.3042190325598,
                    "n_ramp": 1764,
                    "name": "4140",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 200,
                    "v_f": 19.7602072531647,
                    "v_f_leadIn": 19,
                    "v_f_leadOut": 19,
                    "v_f_plunge": 19,
                    "v_f_ramp": 19
                },
                {
                    "description": "",
                    "f_n": 0.012091641057817,
                    "f_z": 0.0031,
                    "guid": "b118ce46-da35-4ed6-9806-b98e05ffe077",
                    "n": 2646.45632854884,
                    "n_ramp": 2646,
                    "name": "Tool Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 300,
                    "v_f": 32.8160584740056,
                    "v_f_leadIn": 32,
                    "v_f_leadOut": 32,
                    "v_f_plunge": 32,
                    "v_f_ramp": 32
                },
                {
                    "description": "",
                    "f_n": 0.01118476797848,
                    "f_z": 0.0028,
                    "guid": "0e1767f5-b0ef-422f-b49d-6cb8c3eb06ed",
                    "n": 3308.0704106860494,
                    "n_ramp": 3308,
                    "name": "Stainless Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 375,
                    "v_f": 37.0503885996837,
                    "v_f_leadIn": 37,
                    "v_f_leadOut": 37,
                    "v_f_plunge": 37,
                    "v_f_ramp": 37
                }
            ]
        },
        "type": "thread mill",
        "unit": "inches",
        "vendor": "Gorilla Mill"
    },
    {
        "BMC": "carbide",
        "description": "generic",
        "geometry": {
            "CSP": false,
            "DC": 0.1875,
            "HAND": true,
            "LB": 0.75,
            "LCF": 0.625,
            "NOF": 4,
            "OAL": 2,
            "SFDM": 0.1875,
            "shoulder-length": 0.75
        },
        "guid": "098ebe1b-a98d-4b52-8c36-ebc4d48206f1",
        "post-process": {
            "break-control": false,
            "comment": "",
            "diameter-offset": 15,
            "length-offset": 15,
            "live": true,
            "manual-tool-change": false,
            "number": 15,
            "turret": 0
        },
        "product-id": "ZA304012",
        "product-link": "4041",
        "start-values": {
            "presets": [
                {
                    "f_n": 0.003926990816987,
                    "f_z": 0.001,
                    "guid": "1cbd5a94-5f45-4547-a0ad-4a0f42ab6638",
                    "n": 8148.733086305044,
                    "n_ramp": 8149,
                    "name": "Default preset",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 400.0000000000001,
                    "v_f": 32.59493234522018,
                    "v_f_leadIn": 32,
                    "v_f_leadOut": 32,
                    "v_f_plunge": 32,
                    "v_f_ramp": 32
                }
            ]
        },
        "type": "flat end mill",
        "unit": "inches",
        "vendor": "Widin"
    },
    {
        "BMC": "carbide",
        "description": "1/2-20 Threadmill",
        "geometry": {
            "CSP": false,
            "DC": 0.398,
            "HAND": true,
            "LB": 1.625,
            "LCF": 0.05,
            "NOF": 4,
            "NT": 1,
            "OAL": 3.5,
            "SFDM": 0.5,
            "TP": 0.05,
            "shoulder-length": 1.575,
            "thread-profile-angle": 60
        },
        "guid": "0da31db5-a122-4bba-987b-c62e681178a2",
        "post-process": {
            "break-control": false,
            "comment": "",
            "diameter-offset": 17,
            "length-offset": 17,
            "live": true,
            "manual-tool-change": false,
            "number": 17,
            "turret": 0
        },
        "product-id": "GMDTTM12-20UN4FL",
        "product-link": "6004",
        "start-values": {
            "presets": [
                {
                    "f_n": 0.007918907882149,
                    "f_z": 0.002,
                    "guid": "805deb43-8fee-4de4-90f4-6f3efe444395",
                    "n": 4798.641500258152,
                    "n_ramp": 4799,
                    "name": "Mild Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 500.0000000000001,
                    "v_f": 38.38913200206522,
                    "v_f_leadIn": 38,
                    "v_f_leadOut": 38,
                    "v_f_plunge": 38,
                    "v_f_ramp": 38
                },
                {
                    "f_n": 0.006946410422937,
                    "f_z": 0.0018,
                    "guid": "a3ba3f21-cee0-4898-945d-82b7994d2a08",
                    "n": 3598.981125193613,
                    "n_ramp": 3599,
                    "name": "Stainless Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 375,
                    "v_f": 25.912664101394014,
                    "v_f_leadIn": 25,
                    "v_f_leadOut": 25,
                    "v_f_plunge": 25,
                    "v_f_ramp": 25
                },
                {
                    "f_n": 0.006772750162364,
                    "f_z": 0.0018,
                    "guid": "4b203a46-ed03-4183-ae50-50ad01180454",
                    "n": 1919.4566001032608,
                    "n_ramp": 1919,
                    "name": "4140",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 200,
                    "v_f": 13.82008752074348,
                    "v_f_leadIn": 13,
                    "v_f_leadOut": 13,
                    "v_f_plunge": 13,
                    "v_f_ramp": 13
                }
            ]
        },
        "type": "thread mill",
        "unit": "inches",
        "vendor": "Gorilla"
    },
Joe Fisher
  • 43
  • 6
  • You need a `public class RootObject { public List Data { get; set; } }` and deserialize into that. Then you can do `foreach (var datum in rootObject.Data) { ... }`. See duplicate. – CodeCaster Nov 02 '21 at 11:05
  • actually have that class as well, just wasn't using it. generated classed with quicktype.io. So issue to you is that I'm not deserializing from top level of Json file? – Joe Fisher Nov 02 '21 at 11:16
  • Some clarity is given thank you. Still have issues with this though. now that i reference the top level, i cannot access Rootobject.datum.SOMETHING. how can i accomplish this. i get a long list of errors saying that my rootobject doesnt contain definition for all my variables. – Joe Fisher Nov 02 '21 at 11:33
  • 1
    figured it out. thank you! using var datum as datum.datumproperty – Joe Fisher Nov 02 '21 at 11:43

0 Answers0