Пример #1
0
        /// <summary>
        /// Edit the product.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void editProductToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                // If rows count is greater than 1.
                if (dataGridProduct.Rows.Count >= 1)
                {
                    // Directory validation.
                    DirectoryInfo newRootDit = new DirectoryInfo("data");
                    if (!newRootDit.Exists)
                    {
                        newRootDit.Create();
                    }
                    // Creating variables.
                    var rowIndex         = dataGridProduct.SelectedRows;
                    var fileClassManager = new List <FileClass>();
                    // FileStream reader.
                    using (FileStream fs =
                               new FileStream($"{newRootDit.FullName}/{mainDataTreeView.SelectedNode.Text}.json",
                                              FileMode.OpenOrCreate))
                    {
                        using var sReader = new StreamReader(fs);
                        // While loop.
                        while (sReader.Peek() > -1)
                        {
                            string input = sReader.ReadToEnd();
                            if (input == String.Empty)
                            {
                                throw new ArgumentException("File is empty");
                            }
                            // Deserialization.
                            List <FileClass> restoredPerson =
                                JsonSerializer.Deserialize <List <FileClass> >(input ?? string.Empty);
                            fileClassManager.AddRange(restoredPerson);
                        }
                    }

                    isClosedForm = false;
                    // Foreach loop validation.
                    var newListFileClasses = fileClassManager.Where(file => file.UCN == rowIndex[0].Cells[2].Value.ToString()).ToList();
                    // Creating new object AddFileForm.
                    var addProductForm = new AddFileForm {
                        fileClass = newListFileClasses
                    };
                    addProductForm.ShowDialog();
                    if (isClosedForm)
                    {
                        // Adding classes.
                        foreach (var file in fileClassManager.Where(file => file.UCN == rowIndex[0].Cells[2].Value.ToString()))
                        {
                            file.Name        = fileClasses.Name;
                            file.Code        = fileClasses.Code;
                            file.UCN         = fileClasses.UCN;
                            file.Company     = fileClasses.Company;
                            file.Amount      = fileClasses.Amount;
                            file.Cost        = fileClasses.Cost;
                            file.Currency    = fileClasses.Currency;
                            file.Warranty    = fileClasses.Warranty;
                            file.Status      = fileClasses.Status;
                            file.Discount    = fileClasses.Discount;
                            file.Country     = fileClasses.Country;
                            file.PictureBox  = fileClasses.PictureBox;
                            file.Description = fileClasses.Description;
                        }
                        // Adding to DataGrid.
                        for (int i = 0; i < rowIndex.Count; i++)
                        {
                            rowIndex[i].Cells[0].Value  = fileClasses.Name;
                            rowIndex[i].Cells[1].Value  = fileClasses.Code;
                            rowIndex[i].Cells[2].Value  = fileClasses.UCN;
                            rowIndex[i].Cells[3].Value  = fileClasses.Company;
                            rowIndex[i].Cells[4].Value  = fileClasses.Amount;
                            rowIndex[i].Cells[5].Value  = fileClasses.Cost;
                            rowIndex[i].Cells[6].Value  = fileClasses.Currency;
                            rowIndex[i].Cells[7].Value  = fileClasses.Warranty ? "Available" : "Unavailable";
                            rowIndex[i].Cells[8].Value  = fileClasses.Status ? "Available" : "Unavailable";
                            rowIndex[i].Cells[9].Value  = fileClasses.Discount;
                            rowIndex[i].Cells[10].Value = fileClasses.Country;
                        }
                    }
                    File.WriteAllText($"{newRootDit.FullName}/{mainDataTreeView.SelectedNode.Text}.json", "");
                    // FileStream.
                    using (var fileStreamWriter =
                               new FileStream($"{newRootDit.FullName}/{mainDataTreeView.SelectedNode.Text}.json",
                                              FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        // Writes and serializes.
                        fileStreamWriter.Position = 0;
                        using var streamWriter    = new StreamWriter(fileStreamWriter);
                        streamWriter.WriteLine(JsonSerializer.Serialize(fileClassManager));
                    }
                }
                else
                {
                    MessageBox.Show(@"Datagrid is empty!");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// Adds the product of node file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addDataProduct_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainDataTreeView.SelectedNode == null)
                {
                    throw new ArgumentException("Node is not selected");
                }

                isClosedForm = false;
                // Creating new form and showing it.
                addFileForm = new AddFileForm();
                addFileForm.ShowDialog();

                if (!isClosedForm)
                {
                    return;
                }

                // Creating new directory.
                DirectoryInfo newRootDit = new DirectoryInfo("data");
                // If root doesn't exist, we create a new one.
                if (!newRootDit.Exists)
                {
                    newRootDit.Create();
                }

                // FileStream function.
                using (var fileStreamWriter = new FileStream($"{newRootDit.FullName}/{mainDataTreeView.SelectedNode.Text}.json", FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    List <FileClass> listClasses = new List <FileClass>();
                    using var streamReader = new StreamReader(fileStreamWriter);
                    // In this loop, code will get all data from file
                    // And add it to new List.
                    while (streamReader.Peek() > -1)
                    {
                        string input = streamReader.ReadToEnd();
                        // Validation.
                        if (input == string.Empty)
                        {
                            throw new ArgumentException("File is empty");
                        }
                        if (input.Length < 10)
                        {
                            throw new ArgumentException("File is empty");
                        }
                        // Deserializing file.
                        var restoredPerson =
                            JsonSerializer.Deserialize <List <FileClass> >(input);
                        // Adding data to new list.
                        if (restoredPerson != null)
                        {
                            listClasses.AddRange(restoredPerson);
                        }
                    }
                    // And the we add the new one
                    // Which was applied by AddFileForm
                    listClasses?.Add(fileClasses);
                    // Going back to first position.
                    fileStreamWriter.Position = 0;
                    using var streamWriter    = new StreamWriter(fileStreamWriter);
                    // Serializing.
                    streamWriter.WriteLine(JsonSerializer.Serialize(listClasses));
                }
                // Refreshing the main dashboard.
                RefreshDashboard();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }