是否可以更改datagridview 末尾空白行的位置?

is it possible to change the position of the blank row at the end of the datagridview?

我想允许用户添加行,但空白行必须位于第一个位置,这样用户无需每次向下滚动即可输入数据以插入新行?!

这就是我在 datagridview 中添加行的方式:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Windows.Forms;

namespace Parc_Automobile
{
    public class SqlHelper
    {
        private const string ConnectionString =
            "Data Source=KIM;Initial Catalog=TestingBlank;Integrated Security=True";

        private readonly DataGridView _dgv;
        private BindingSource _bindingSource;
        private DataTable _dataTable;
        private string _selectQueryString;
        private SqlConnection _sqlConnection;
        private SqlDataAdapter _sqlDataAdapter;

        protected SqlHelper(DataGridView dgv)
        {
            _dgv = dgv;
        }

        //display a table in datagridview
        public void ShowTable(String tableName)
        {
            _sqlConnection = new SqlConnection(ConnectionString);
            _sqlConnection.Open();

            _selectQueryString = "SELECT * FROM " + tableName;
            _sqlDataAdapter = new SqlDataAdapter(_selectQueryString, _sqlConnection);
            var sqlCommandBuilder = new SqlCommandBuilder(_sqlDataAdapter);
            _dataTable = new DataTable();
            _sqlDataAdapter.Fill(_dataTable);
            _bindingSource = new BindingSource {DataSource = _dataTable};

            var tempRow = this._dataTable.NewRow();
            this._dataTable.Rows.InsertAt(tempRow, 0);

            _dgv.DataSource = _bindingSource;
            _sqlConnection.Close();
        }

        //Search In datagridview using input of users
        public void SearchTable(String SearchText, String columnNameToSearch, int type)
        {
            try
            {
                var filterBuilder = "";
                switch (type)
                {
                    case TEXT_COLUMN:
                        filterBuilder = columnNameToSearch + " LIKE '" + SearchText + "%'";
                        break;

                    case NUMERIC_COLUMN:
                        filterBuilder = columnNameToSearch + " = '" + SearchText + "'";
                        break;
                }
                var bs = new BindingSource
                {
                    DataSource = _dgv.DataSource,
                    Filter = filterBuilder
                };
                _dgv.DataSource = bs;
            }
            catch (Exception e)
            {
                // ignored
            }
        }

        public void DeleteRow()
        {
            if (_dgv.CurrentRow != null) _dgv.Rows.RemoveAt(_dgv.CurrentRow.Index);
            _sqlDataAdapter.Update(_dataTable);
        }

        public void SaveChanges(Label label)
        {
            try
            {
                _sqlDataAdapter.Update(_dataTable);
                label.Text = "Changement a été effectué avec succès.";
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }
    }
}

添加新行时使用 Insert 方法而不是 AddInsert 允许为新行指定索引。将索引设置为 0:

this.dataGridView1.Rows.Insert(0,new DataGridViewRow());

编辑我

调整您评论的答案:如果您使用数据源绑定,则必须使用该数据源创建一个新行。然后将其插入到指定位置。假设您有 myDataSet 数据源,其中包含 Log table。此代码将创建一个新行并将其作为第一行插入:

var tempRow = this.myDataSet.Log.NewRow();
this.myDataSet.Log.Rows.InsertAt(tempRow, 0);

您的 DataGridView 将适当更新。

编辑二

如果您尝试分配不同于 table 中的列类型的数据类型,您将遇到异常。假设 ID 列是整数,但您尝试添加 string。要处理数据错误,您必须处理 DataError 事件。下面是一个使用您的代码的示例。

protected SqlHelper(DataGridView dgv)
{
    _dgv = dgv;
    _dgv.DataError += _dgv_DataError;
}

void _dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    if (e.Exception != null)
    {
        MessageBox.Show(string.Format("Incorrect data: {0}", e.Exception.Message) );
        e.Cancel = false;

        // handle the exception
    }
}