如何删除最后一行并在新选定行之前创建一行? [英] How do I delete last row and make row before it the new selected row?

查看:115
本文介绍了如何删除最后一行并在新选定行之前创建一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有DataGridViewControl的C#.NET WinForms应用程序。我在整数变量"intIndex"中使用跟踪选中DataGridView中的哪一行。删除选定的行时,默认情况下第一行将成为新选定的行。
假设我有一个包含10行的DatagridView。我需要改变这种行为如下......

I'm writing a C# .NET WinForms app with a DataGridViewControl. I'm using in integer variable "intIndex" to keep track of which row in the DataGridView is seleted. When a selected row is deleted, the first row becomes the new selected row by default. Say I have a DatagridView with 10 rows. I need to alter this behavior as follows...

1。删除第10行时,第9行将成为新选定的行。

1. When row 10 is deleted, row 9 becomes the new selected row.

2。当删除最后一行之前的任何行时,删除的行之后的行将成为新选择的行。

2. When any row before the last row is deleted, the row after the deleted row becomes the new selected row.

我有以下代码在删除行或行时更新intIndex用户使用CellContentClicked事件选择...

I have the following code to update intIndex when a row is deleted or a row is selected by the user with the CellContentClicked event...

                if (dgvQuestions.RowCount > 0)
                    intIndex = dgvQuestions.SelectedRows[0].Index;

此代码当前位于DataGridView的SelectionChanged事件处理程序中。我也在RowsRemoved事件中尝试过它。在这两种情况下,当我删除一行时,它会抛出错误...

This code is currently in the DataGridView's SelectionChanged event handler. I also tried it in the RowsRemoved event. In both cases, when I delete a row, it throws an error...

System.ArgumentOutOfRangeException

HResult = 0x80131502

消息=索引超出范围。必须是非负数且小于集合的大小。



我在哪里可以将此代码放在哪里?

System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Index was out of range. Must be non-negative and less than the size of the collection.

Where can I put this code where it will work?

推荐答案

我建议通过List或DataTable等加载数据并使用BindingSource。

I would suggest loading data via a List or DataTable etc and use a BindingSource .

using System.Windows.Forms;
namespace MessageDialogs
{
    public static class KarenDialogs
    {
        public static bool Question(string Text)
        {
            return (MessageBox.Show(Text, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MessageDialogs;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        BindingSource bs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bs.DataSource = new List<Person>()
            {
                new Person() { FirstName = "Jim", LastName ="Adams" },
                new Person() { FirstName = "Anne", LastName ="Smith" },
                new Person() { FirstName = "Mary", LastName ="Smith" },
                new Person() { FirstName = "Bill", LastName ="Smith" },
                new Person() { FirstName = "Sean", LastName ="Adams" }
            };

            dataGridView1.DataSource = bs;
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (bs.Current != null)
            {
                if (KarenDialogs.Question(


" Remove {((Person)bs.Current).FirstName}"))
{
bs.RemoveCurrent() ;
}
}
}
}
公共类人员
{
公共字符串FirstName {get;组; }
公共字符串LastName {get;组; }
}
}
"Remove {((Person)bs.Current).FirstName}")) { bs.RemoveCurrent(); } } } } public class Person { public string FirstName { get; set; } public string LastName { get; set; } } }


这篇关于如何删除最后一行并在新选定行之前创建一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆