访问DataGridView按行添加顺序

Rif*_*ifu 6 c# collections datagridview row .net-3.5

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

如果我收到"0;1;2"三个第一行将分别用purle,green和red着色.
当我对数据网格点击列标题进行排序时出现问题:颜色以相同的方式应用.

例如 :

Names|Labels  
Name1|Label1  
Name2|Label2  
Name3|Label3 
Run Code Online (Sandbox Code Playgroud)

我收到的"0;1;2"意思是"Purple;Green;Red":

Names|Labels  
Name1|Label1 => Purple  
Name2|Label2 => Green  
Name3|Label3 => Red 
Run Code Online (Sandbox Code Playgroud)

我排序(降序):

Names|Labels  
Name3|Label3 => Red  
Name2|Label2 => Green  
Name1|Label1 => Purple  
Run Code Online (Sandbox Code Playgroud)

我收到的"3;4;5"意思是"Yellow;Orange;Pink":

Names|Labels  
Name3|Label3 => Yellow  
Name2|Label2 => Orange  
Name1|Label1 => Pink 
Run Code Online (Sandbox Code Playgroud)

但这不是我在等待的,我想要的是:

Names|Labels  
Name3|Label3 => Pink  
Name2|Label2 => Orange  
Name1|Label1 => Yellow  
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

难道没有办法按行在DataGridView中添加它们的顺序访问行吗?

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

=====这是我用过的解决方案,感谢LarsTech的回答 =====

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

foreach(System.Windows.Forms.DataGridViewRow row in Rows)
{
  row.Tag = row.Index;
}
Run Code Online (Sandbox Code Playgroud)

然后我可以使用此标记作为行号:

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;
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Lar*_*ech 4

您可以尝试使用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();
}
Run Code Online (Sandbox Code Playgroud)

这是我对你的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];
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我首先循环遍历分割状态字符串以获取行索引并将实际值转换为颜色索引。然后,我循环遍历行以查找我放置在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];
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

显然需要错误检查。

  • 由于标记属性是一个对象,因此我认为没有任何理由将标记存储为字符串。您可能想尝试使用“dataGridView1.Rows[i].Tag = i”,然后使用“int rowIndex = i”代替 Tostring() 和 Convert.ToInt 版本。 (2认同)