Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 如何在C中使用DataGridView值作为保存和加载控件?_C#_Datagridview - Fatal编程技术网

C# 如何在C中使用DataGridView值作为保存和加载控件?

C# 如何在C中使用DataGridView值作为保存和加载控件?,c#,datagridview,C#,Datagridview,我目前正在使用DataGridView制作保存和加载选项程序。“保存”按钮将保存picturebox坐标,以便根据picturebox上已绘制的内容绘制线条。多个保存数据保存为DataGridView中的表。加载后,表格中保存的坐标返回屏幕并绘制保存的坐标线。将来,我们将使保存的数据可以导出并导入excel文件,但目前这些按钮还不能工作 问题是如何使多个加载成为可能?第一次保存数据可以在单击后正确加载,但第二次和下一次将使应用程序崩溃,因此唯一有效的保存数据是最上面的数据。错误始终指向字符串li

我目前正在使用DataGridView制作保存和加载选项程序。“保存”按钮将保存picturebox坐标,以便根据picturebox上已绘制的内容绘制线条。多个保存数据保存为DataGridView中的表。加载后,表格中保存的坐标返回屏幕并绘制保存的坐标线。将来,我们将使保存的数据可以导出并导入excel文件,但目前这些按钮还不能工作

问题是如何使多个加载成为可能?第一次保存数据可以在单击后正确加载,但第二次和下一次将使应用程序崩溃,因此唯一有效的保存数据是最上面的数据。错误始终指向字符串lineCell=dataGridView1。SelectedRows[rowIndex]。单元格[1]。Value.ToString;但我不知道如何修复它

此外,如果有建议如何导出和导入到excel文件,请告诉我。多谢各位

这个应用程序大致是这样的

用于datagridview中加载和删除按钮的代码

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;
            int rowIndex = dataGridView1.CurrentCell.RowIndex;
            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                if (e.ColumnIndex == dataGridView1.Columns[7].Index)
                {
                    string lineCell = dataGridView1.SelectedRows[rowIndex].Cells[1].Value.ToString();
                    int x1Cell = Convert.ToInt16(dataGridView1.SelectedRows[rowIndex].Cells[3].Value.ToString());
                    int y1Cell = Convert.ToInt16(dataGridView1.SelectedRows[rowIndex].Cells[4].Value.ToString());
                    int x2Cell = Convert.ToInt16(dataGridView1.SelectedRows[rowIndex].Cells[5].Value.ToString());
                    int y2Cell = Convert.ToInt16(dataGridView1.SelectedRows[rowIndex].Cells[6].Value.ToString());
                    if (lineCell == "Black")
                    {
                        lineComboBox.SelectedIndex = 0;
                        line1X1 = x1Cell;
                        line1Y1 = y1Cell;
                        line1X2 = x2Cell;
                        line1Y2 = y2Cell;
                    }
                    else if (lineCell == "Red")
                    {
                        lineComboBox.SelectedIndex = 1;
                        line2X1 = x1Cell;
                        line2Y1 = y1Cell;
                        line2X2 = x2Cell;
                        line2Y2 = y2Cell;
                    }
                    else if (lineCell == "Yellow")
                    {
                        lineComboBox.SelectedIndex = 2;
                        line3X1 = x1Cell;
                        line3Y1 = y1Cell;
                        line3X2 = x2Cell;
                        line3Y2 = y2Cell;
                    }
                        lineComboBox.Refresh();
                        pictureBox1.Refresh();
                }
                else if (e.ColumnIndex == dataGridView1.Columns[8].Index)
                {
                    dataGridView1.Rows.RemoveAt(rowIndex);
                }
            }
保存按钮代码

private void saveButton_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.Rows.Add();
            dataGridView1.Rows[index].Cells[0].Value = "Save " + index.ToString();
            dataGridView1.Rows[index].Cells[2].Value = ang;
            if(lineComboBox.SelectedIndex == 0)
            {
                dataGridView1.Rows[index].Cells[1].Value = "Black";
                dataGridView1.Rows[index].Cells[3].Value = line1X1;
                dataGridView1.Rows[index].Cells[4].Value = line1Y1;
                dataGridView1.Rows[index].Cells[5].Value = line1X2;
                dataGridView1.Rows[index].Cells[6].Value = line1Y2;
            }
            else if(lineComboBox.SelectedIndex == 1)
            {
                dataGridView1.Rows[index].Cells[1].Value = "Red";
                dataGridView1.Rows[index].Cells[3].Value = line2X1;
                dataGridView1.Rows[index].Cells[4].Value = line2Y1;
                dataGridView1.Rows[index].Cells[5].Value = line2X2;
                dataGridView1.Rows[index].Cells[6].Value = line2Y2;
            }
            else if (lineComboBox.SelectedIndex == 2)
            {
                dataGridView1.Rows[index].Cells[1].Value = "Yellow";
                dataGridView1.Rows[index].Cells[3].Value = line3X1;
                dataGridView1.Rows[index].Cells[4].Value = line3Y1;
                dataGridView1.Rows[index].Cells[5].Value = line3X2;
                dataGridView1.Rows[index].Cells[6].Value = line3Y2;
            }
        }
这是错误信息。对不起,它是日文的,办公室只使用日文版本。但大致翻译为

mscorlib.dll中发生“System.ArgumentOutOfRangeException”类型的未处理异常 其他信息:索引超出范围。必须为非负数且小于集合的大小。

你能分享你收到的错误信息吗?@ChetanRanpariya添加了错误信息,但很抱歉是日语。已翻译dataGridView1。SelectedRows[rowIndex]或dataGridView1。SelectedRows[rowIndex]。单元格[1]触发了异常。检查dataGridView1.SelectedRows.Count是否低于rowIndex。检查dataGridView1.SelectedRows[rowIndex].Cells.Count小于1。@抱歉,我不明白。你能给我更详细的说明吗?非常感谢您,您可以在调试模式下查看悬停在其上的变量,特别是您尝试选择的行与DataGridView中显示的行。还要注意的是,对于这样的场景,最好使用设置为DataGridView数据源的DataTable,而不是直接从DataGridView获取的值。它更快、更安全,即定义了数据类型。如果你要画几百条线,DGV将是一个巨大的倒退。