c# - 按照添加顺序访问 DataGridView 行

标签 c# collections .net-3.5 datagridview row

我有一个 DataGridView,它在设置 States 属性时为其行着色。
States 是一个字符串,表示以分号分隔的数字列表。

如果我收到 "0;1;2",前三行的颜色将分别为紫色、绿色和红色。
当我单击列标题对数据网格进行排序时,问题就来了:颜色的应用方式相同。

例如:

Names|Labels  
Name1|Label1  
Name2|Label2  
Name3|Label3 

我收到 "0;1;2" 这意味着 "Purple;Green;Red" :

Names|Labels  
Name1|Label1 => Purple  
Name2|Label2 => Green  
Name3|Label3 => Red 

我排序(降序):

Names|Labels  
Name3|Label3 => Red  
Name2|Label2 => Green  
Name1|Label1 => Purple  

我收到 "3;4;5" 这意味着 "Yellow;Orange;Pink" :

Names|Labels  
Name3|Label3 => Yellow  
Name2|Label2 => Orange  
Name1|Label1 => Pink 

但这不是我想要的,我想要的是:

Names|Labels  
Name3|Label3 => Pink  
Name2|Label2 => Orange  
Name1|Label1 => Yellow  

这是我的代码:

protected String m_States;

public virtual String States
{
  get { return m_States; }

  set {
    m_States = value;
    if (m_bRunning)
    {
      UpdateColors();
    }
  }
}

private void UpdateColors()
{
  String[] sStates = new String[] { };
  if (m_States != null)
  {
    sStates = m_States.Split(m_sSeparators);

    int nState = 0;
    int nRowNumber = 0;
    foreach (System.Windows.Forms.DataGridViewRow row in Rows)
    {
      nState = int.Parse(sStates[nRowNumber]);

      if (nState < 0 || nState > m_Couleurs_Fond_Etats.Length)
      {
        nState = m_Couleurs_Fond_Etats.Length - 1;
      }
      row.DefaultCellStyle.BackColor = m_Couleurs_Fond_Etats[nState];
      row.DefaultCellStyle.ForeColor = m_Couleurs_Texte_Etats[nState];
      row.DefaultCellStyle.SelectionBackColor = m_Couleurs_Sel_Fond_Etats[nState];
      row.DefaultCellStyle.SelectionForeColor = m_Couleurs_Sel_Texte_Etats[nState];

      nState = 0;
      ++nRowNumber;
    }
  }
}

有没有一种方法可以按照行添加到 DataGridView 中的顺序访问行?

PS : 我第一次使用 row.Index 而不是 nRowNumber,所以我认为问题来自于此,但显然,Rows 集合被重组或 foreach 根据 rowIndexes 解析它。

===== 这是我使用的解决方案,感谢 LarsTech's answer =====

添加行后,我以这种方式标记它们:

foreach(System.Windows.Forms.DataGridViewRow row in Rows)
{
  row.Tag = row.Index;
}

然后我可以使用这个标签作为行号:

private void UpdateColors()
{
  String[] sStates = new String[] { };
  if (m_States != null)
  {
    sStates = m_States.Split(m_sSeparators);

    int nState = 0;
    int nRowNumber = 0;
    foreach (System.Windows.Forms.DataGridViewRow row in Rows)
    {
      nRowNumber = Convert.ToInt32(row.Tag);
      if (nRowNumber >= 0 && nRowNumber < sEtats.Length)
      {
        nState = int.Parse(sStates[nRowNumber]);

        if (nState < 0 || nState > m_Couleurs_Fond_Etats.Length)
        {
          nState = m_Couleurs_Fond_Etats.Length - 1;
        }
        row.DefaultCellStyle.BackColor = m_Couleurs_Fond_Etats[nState];
        row.DefaultCellStyle.ForeColor = m_Couleurs_Texte_Etats[nState];
        row.DefaultCellStyle.SelectionBackColor = m_Couleurs_Sel_Fond_Etats[nState];
        row.DefaultCellStyle.SelectionForeColor = m_Couleurs_Sel_Texte_Etats[nState];

        nState = 0;
      }
    }
  }
}

最佳答案

您可以尝试使用该行的 Tag 属性来放置您的行索引号。这就是我初始化 DataGridView 的方式:

dataGridView1.Rows.Add(3);
for (int i = 0; i < 3; i++) {
  dataGridView1.Rows[i].Tag = i;
  dataGridView1.Rows[i].Cells[0].Value = "Name " + i.ToString();
  dataGridView1.Rows[i].Cells[1].Value = "Label " + i.ToString();
}

这是我的UpdateColors 例程版本:

private void UpdateColors() {
  String[] sStates = new String[] { };
  if (m_States != null) {
    sStates = m_States.Split(';');
    for (int i = 0; i < sStates.Length;i++) {
      int nState = Convert.ToInt32(sStates[i]);
      foreach (DataGridViewRow row in dataGridView1.Rows) {
        int rowIndex = Convert.ToInt32(row.Tag);
        if (rowIndex == i) {
          row.DefaultCellStyle.BackColor = m_Couleurs_Fond_Etats[nState];
        }
      }
    }
  }
}

我首先循环遍历您的拆分状态字符串以获取行索引并将实际值转换为颜色索引。然后我遍历行以找到我放置在 Tag 属性中的匹配索引属性。

这是一个只使用一个循环的清理版本:

private void UpdateColors() {
  String[] sStates = new String[] { };
  if (m_States != null) {
    sStates = m_States.Split(';');
    foreach (DataGridViewRow row in dataGridView1.Rows) {
      int rowIndex = Convert.ToInt32(row.Tag);
      int colorIndex = Convert.ToInt32(sStates[rowIndex]);
      row.DefaultCellStyle.BackColor = m_Couleurs_Fond_Etats[colorIndex];
    }
  }
}

显然需要错误检查。

关于c# - 按照添加顺序访问 DataGridView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11153277/

相关文章:

c# - 如何在 C# 2012 版中正确清理 Excel 互操作对象

c# - 推荐哪个: "static public" or "public static"

c# - C# 中 pretty-print 异常

javascript - Symfony 2 中的 ArrayCollection(表单集合)索引冲突

Java,阻塞队列

c# - 使用linq to xml用单引号保存xml元素的属性值

c# - ASP.NET 3.5 网络表单的日期时间选择器

c# - 并非所有路径都返回值-Windows Form App-C#

c# - 在用户和服务器之间传递数据

java - 优化大型集合的垃圾收集