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

时间:2015-03-23 00:42:01

标签: c# winforms c#-4.0 datagridview

我想允许用户添加行,但空白行必须位于第一个位置,让用户输入数据而不是每次都向下滚动以插入新行?!

http://i.stack.imgur.com/YDh6o.png

这就是我在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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

编辑我

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

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

您的DataGridView将得到适当更新。

编辑II

例如,如果尝试在表中分配不同于列类型的数据类型,则会出现异常。假设ID列为int,但您尝试添加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
    }
}
相关问题