排序时如何在行标题中维护自动编号 [英] How to maintain autonumber in rows header when sorting

查看:79
本文介绍了排序时如何在行标题中维护自动编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



i有未绑定的datagridview,我在行标题中添加了自动编号。

我还在第一列单元格中添加了自动编号但是让我们专注于行标题。

问题是当我按某些列的值对网格进行排序时,行标题中的数字会丢失顺序。

准确地说我想拥有1 ,2,3,4,5 ...按顺序排在行标题中,不管我在网格上做什么排序。

尝试通过再次更改网格中的行标题来寻找解决方案事件'Sorted'和'SortCompare',但没有运气。

这是我的代码在初始化我在列表中添加一些名称,在前两列中添加自动编号,并在行标题中添加自动编号。 br $>


Hello,

i have unbound datagridview and i added autonumber in rows header.
I also added autonumber in first column cells but lets focus just on rows header.
Problem is when i sort the grid by values from some column the numbers in rows header lose order.
To be precise i want to have 1,2,3,4,5...ALWAYS in row header in that order,no mather what sorting i am doing on the grid.
Tried to find solution by changing again the row headers in grids events 'Sorted' and 'SortCompare' but with no luck.
Here is my code where on Initialisation i am adding some names from the list and autonumbers in first two columns and adding autonumbers to row header.

public Form3(List<string>; list, int brojkola)
{
   InitializeComponent();
   this.WindowState = FormWindowState.Maximized;

   dodajkolone(brojkola);

   foreach (string igrac in list)
   {
      this.dataGridView1.Rows.Add();
      this.dataGridView1.Rows[list.IndexOf(igrac)].Cells[1].Value = igrac;
      int rednibroj = list.IndexOf(igrac);
      this.dataGridView1.Rows[rednibroj].Cells[0].Value = rednibroj + 1;
      setRowNumber(dataGridView1);
   }

   dataGridView1.Refresh();
}

private void setRowNumber(DataGridView dgv)
{
   int broj = 1;
   foreach (DataGridViewRow r in dgv.Rows)
   {
      r.HeaderCell.Value = (broj).ToString();
      //dgv.Rows[r.Index].HeaderCell.Value = (r.Index + 1).ToString();
      broj++;
   }
}





有什么办法可以防止第一列在用户排序时排序列或防止行标题被排序并保持总是像自动编号1,2,3,4 ...或者是否有任何方式排序后,在一些事件中放置一些代码,以显示行标题中的数字顺序我我应该使用什么事件。



Is there any way to prevent first column to be sorted when user sort on some other column or to prevent rows header to be sorted and stay always like autonumber 1,2,3,4...or is there any way after sorting by a column to put some code in some event to display numbers in row header in order i want.What event i should use for that.

推荐答案

如果你总是想在你的标题中使用相同的数字,这很容易。

所以如果你想:

If you always want the same numbers in your how headers, that's pretty easy.
So if you want:
1  AAA
2  AAB
3  BAA
4  CAA



And

1  CAA
2  BAA
3  AAB
4  AAA

然后只处理DGV的Sorted事件:

Then just handle the Sorted event for the DGV:

private void myDataGridView_Sorted(object sender, EventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null)
        {
        ReNumberRows(dgv);
        }
    }

private void ReNumberRows(DataGridView dgv)
    {
    int i = 0;
    foreach (DataGridViewRow row in dgv.Rows)
        {
        row.HeaderCell.Value = (++i).ToString();
        }
    }


非常感谢!



As我已经说过了:尝试通过再次更改网格事件中的行标题'排序'和'SortCompare'来找到解决方案,但没有运气。



试图重新排序datagreedview排序事件中的行标题,但我想我做错了。
Thank you so much!

As i already say :"Tried to find solution by changing again the row headers in grids events 'Sorted' and 'SortCompare' but with no luck."

tried to reorder rows header in the datagreedview Sorted event but i guess i did something wrong.


这篇关于排序时如何在行标题中维护自动编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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