0

So I'm trying out some logic. It's not going well.

My problem, for now, is when reading a local XML document into the dataGridView. (LoadXmlButton_Click method)

Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace AddProducts_XMLForms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public DataTable dst = new DataTable();
        Guid id = Guid.NewGuid();


        private void Form1_Load(object sender, EventArgs e)
        {
            dst.Columns.Add("Artikelnummer", typeof(string));
            dst.Columns.Add("Kategori", typeof(string));
            dst.Columns.Add("Beskrivning", typeof(string));
            dst.Columns.Add("Pris", typeof(decimal));
            dst.Columns.Add("Bildadress", typeof(string));
        }

        private void loadXmlButton_Click(object sender, EventArgs e)
        {
            try
            {
                dst.ReadXml("products.xml");
                productsDataGridView.DataSource = dst;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void exportXmlButton_Click(object sender, EventArgs e)
        {
            try
            {
                dst = (DataTable)productsDataGridView.DataSource;
                dst.TableName = "Product";
                dst.WriteXml("products.xml", true);
                xmlExportedLabel.Text = "OK";
                xmlExportedLabel.ForeColor = Color.Green;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                dst.Rows.RemoveAt(productsDataGridView.CurrentCell.RowIndex);
                productsDataGridView.DataSource = dst;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void addProductButton_Click_1(object sender, EventArgs e)
        {
            try
            {
                dst.Rows.Add(id.ToString(), categoryCheckListBox.SelectedItem, descriptionTextBox.Text, priceTextBox.Text, imageUrlTextBox.Text);
                productsDataGridView.DataSource = dst;

                foreach (int i in categoryCheckListBox.CheckedIndices)
                {
                    categoryCheckListBox.SetItemCheckState(i, CheckState.Unchecked);
                }
                categoryCheckListBox.ClearSelected();
                priceTextBox.Clear();
                imageUrlTextBox.Clear();
                descriptionTextBox.Clear();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        int i;
        private void editButton_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = productsDataGridView.Rows[i];
                row.Cells[1].Value = categoryCheckListBox.SelectedItem;
                row.Cells[2].Value = priceTextBox.Text;
                row.Cells[3].Value = descriptionTextBox.Text;
                row.Cells[4].Value = imageUrlTextBox.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

It just won't load the products in the XML and place them where i want them and I'm not that good at debugging yet but it seems that if rewrite the code in loadXmlButton_Click:

private void loadXmlButton_Click(object sender, EventArgs e)
        {
            try
            {
                var dt = new DataSet();
                dt.ReadXml("products.xml");
                productsDataGridView.DataSource = dt.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

It will now load into the dataGridView properly but then my delete method wont work. Perhaps my delete method is the main problem? I'm confused!

    private void deleteButton_Click(object sender, EventArgs e)
    {
        try
        {
            dst.Rows.RemoveAt(productsDataGridView.CurrentCell.RowIndex);
            productsDataGridView.DataSource = dst;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Awots
  • 75
  • 8
  • Show MessageBox in try region to check whether deleteButton_Click execute or not? – Charlie Nov 11 '15 at 10:01
  • Can you specify? I have exception handler for deleteButton_Click and it executes. Exception: "There is no row at Index 0." @Charlie – Awots Nov 11 '15 at 10:14
  • Use MessageBox(productsDataGridView.Rows.Count); to check whether it contains any row – Charlie Nov 11 '15 at 10:25
  • The structure of the xml document created by `CreateXML()` method is different from the data table initialized in your `Form_Load` (different column names). So which is the correct one? – Ivan Stoev Nov 11 '15 at 12:16

1 Answers1

0

1)There is a chance that dst is empty which is causing Exception.

2) You need to select some cell before firing delete event. Error is probably because CurrentCell is null.

Use these line if you want to assign CurrentCell to some object.

    DataGridViewCell cell = productsDataGridView[1, 1];
    productsDataGridView.CurrentCell = cell;

Ensure if CurrentCell is not null.

 if(dst.Rows.Count>0&&productsDataGridView.CurrentCell!=null)
 {
   try
    {
        dst.Rows.RemoveAt(productsDataGridView.CurrentCell.RowIndex);
        productsDataGridView.DataSource = dst;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
 }
Charlie
  • 4,827
  • 2
  • 31
  • 55