重置列增量datagridview

时间:2014-12-27 21:20:42

标签: c# winforms datagridview

我在删除一行或多行后,尝试更新DataGridView应用程序中WinForms的行增量编号。我查看了源代码以及如何将增量添加到DataTable中的列的所有要点。我的DataGridView绑定到我的DataTable,并绑定到DataSet。

我是如何通过数据表创建的:

DataColumn itemNumber = new DataColumn();
itemNumber.ColumnName = "ItemNumber";
itemNumber.AutoIncrement = true;
itemNumber.AutoIncrementSeed = 1;
itemNumber.AutoIncrementStep = 1;

DataColumn article = new DataColumn();
article.ColumnName = "Article";
article.ReadOnly = true;

DataColumn description = new DataColumn();
description.ColumnName = "Description";
description.ReadOnly = true;

DataColumn type = new DataColumn();
type.ColumnName = "Type";
type.ReadOnly = true;

//add to datatable
dt.Columns.Add(itemNumber);
dt.Columns.Add(article);
dt.Columns.Add(description);
dt.Columns.Add(type);

从DataGridView中删除行

foreach (DataGridViewRow row in dgvView.SelectedRows)
{
    dgvView.Rows.RemoveAt(row.Index);
}

如果我有5行,则删除一行。我希望增量值从1,2,3,4等开始......

有人能指出我如何实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

我无法想出一种有效的方法,但是这里有一些可以使用小数据集的想法。

自己跟上价值观

1)不要将列定义为AutoIncrement列:

DataColumn itemNumber = new DataColumn();
itemNumber.ColumnName = "ItemNumber";
//itemNumber.AutoIncrement = true;
//itemNumber.AutoIncrementSeed = 1;
//itemNumber.AutoIncrementStep = 1;

2)处理DataGridView.RowsAddedDataGridView.RowsRemoved事件以及"重置"价值观:

private void dgvView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    if (dgvView.Columns.Contains("ItemNumber"))
    {
        foreach (DataGridViewRow r in dgvView.Rows)
        {
            r.Cells["ItemNumber"].Value = r.Index + 1;
        }
    }
}

private void dgvView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    if (dgvView.Columns.Contains("ItemNumber"))
    {
        foreach (DataGridViewRow r in dgvView.Rows)
        {
            r.Cells["ItemNumber"].Value = r.Index + 1;
        }
    }
}

通过重建DataTable并重新绑定到

来重新生成值

1)定义下面的辅助方法:

private DataTable ResetAutoIncrementColumn(DataTable dt, string autoIncrementColumnName)
{
    DataTable result = new DataTable();
    DataColumn itemNumber = new DataColumn(autoIncrementColumnName);
    itemNumber.AutoIncrement = true;
    itemNumber.AutoIncrementSeed = 1;
    itemNumber.AutoIncrementStep = 1;
    result.Columns.Add(itemNumber);

    dt.Columns.Remove(autoIncrementColumnName);
    result.Merge(dt, true);
    return result;
}

2)在适当的时候(例如在原始问题中进行一系列删除之后)调用它:

dt = ResetAutoIncrementColumn(dt, "ItemNumber");
dt.Columns["ItemNumber"].SetOrdinal(0);
dgvView.DataSource = dt;
dgvView.Columns["ItemNumber"].DisplayIndex = 0;

如果您只是在寻找视觉行号

此外,如果您只是希望在DataGridView上有一个可视行号(并且您不关心底层DataTable中存在的值),那么您可以处理DataGridView.RowPostPaint事件如下(从https://stackoverflow.com/a/12840794/3085273拉出):

private void dgGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    var grid = sender as DataGridView;
    var rowIdx = (e.RowIndex + 1).ToString();

    var centerFormat = new StringFormat() 
    { 
        // right alignment might actually make more sense for numbers
        Alignment = StringAlignment.Center, 
        LineAlignment = StringAlignment.Center
    };

    var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
    e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}