【问题标题】:is it possible to change the position of the blank row at the end of the datagridview?是否可以更改 datagridview 末尾空白行的位置?
【发布时间】:2015-03-23 00:42:00
【问题描述】:

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

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

【问题讨论】:

    标签: c# winforms c#-4.0 datagridview


    【解决方案1】:

    当您添加新行时,请使用 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 将适当更新。

    编辑二

    如果您尝试分配与表中的列类型不同的数据类型,您将遇到异常。假设 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
        }
    }
    

    【讨论】:

    • 我已在表单的构造函数中添加了此代码,但出现错误:当控件绑定数据时,无法以编程方式将行添加到 DataGridView 的行集合中。
    • 在这种情况下,您必须在数据源上创建行并在该数据行上插入具有指定位置的新行。我会在一分钟内添加一个编辑。
    • 很抱歉打扰你我试图应用你的新答案,但我没有在我的代码中使用数据集,你能帮我吗?我在问题中添加了如何显示行对不起我是绝对的初学者
    • 其实你可以。你有你的sqlConnectiondataTable。我现在无法对其进行测试,但我想说的是,在添加新行时,您必须打开连接并使用 dataTable 创建一行,如我的示例所示。
    • 我现在只是在代码中测试它,该行在顶部显示为空白但没有“*”表示用户可以输入数据,我尝试输入数据但我得到:对象不能从 DBNull 转换为其他类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多