Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在dataGridView中删除行索引时出现问题_C#_Winforms_Datagridview_Readxml - Fatal编程技术网

C# 在dataGridView中删除行索引时出现问题

C# 在dataGridView中删除行索引时出现问题,c#,winforms,datagridview,readxml,C#,Winforms,Datagridview,Readxml,所以我在尝试一些逻辑。事情进展不顺利 目前,我的问题是在将本地XML文档读入dataGridView时。 (LoadXmlButton_点击法) 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.

所以我在尝试一些逻辑。事情进展不顺利

目前,我的问题是在将本地XML文档读入dataGridView时。 (LoadXmlButton_点击法)

这是我的密码:

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);
            }
        }
    }
}
它只是不会将产品加载到XML中,并将其放置在我想要的位置,而且我还不太擅长调试,但如果在loadXmlButton_中重写代码,请单击:

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);
            }
        }
现在它将正确地加载到dataGridView中,但是我的delete方法无法工作。也许我的删除方法是主要问题?我糊涂了

    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);
        }
    }
1) dst可能为空,这会导致异常

2) 在触发删除事件之前,需要选择一些单元格。错误可能是因为CurrentCell为空

如果要将CurrentCell指定给某个对象,请使用这些行

    DataGridViewCell cell = productsDataGridView[1, 1];
    productsDataGridView.CurrentCell = cell;
确保CurrentCell是否不为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);
    }
 }

在“尝试”区域中显示MessageBox以检查deleteButton\u是否单击“执行”?是否可以指定?我有deleteButton\u Click的异常处理程序,它会执行。异常:“索引0处没有行。”@CharlieUse MessageBox(productsDataGridView.Rows.Count);要检查它是否包含任何行,由
CreateXML()
方法创建的xml文档的结构不同于在
表单\u Load
中初始化的数据表(不同的列名)。那么哪一个是正确的呢?