C# 按添加顺序访问DataGridView行
我有一个DataGridView,它在设置States属性时为其行着色。C# 按添加顺序访问DataGridView行,c#,collections,.net-3.5,datagridview,row,C#,Collections,.net 3.5,Datagridview,Row,我有一个DataGridView,它在设置States属性时为其行着色。 States是一个字符串,表示以分号分隔的数字列表 如果我收到“0;1;2”第一行的三个颜色分别为绿色、绿色和红色。 当我单击列标题对datagrid进行排序时,问题就出现了:颜色的应用方式是相同的 例如: Names|Labels Name1|Label1 Name2|Label2 Name3|Label3 我收到“0;1;2”,意思是“紫色;绿色;红色”: I排序(降序): 我收到“3;4;5”,意思是
States是一个字符串,表示以分号分隔的数字列表 如果我收到
“0;1;2”
第一行的三个颜色分别为绿色、绿色和红色。当我单击列标题对datagrid进行排序时,问题就出现了:颜色的应用方式是相同的 例如:
Names|Labels
Name1|Label1
Name2|Label2
Name3|Label3
我收到“0;1;2”
,意思是“紫色;绿色;红色”
:
I排序(降序):
我收到“3;4;5”
,意思是“黄色;橙色;粉色”
:
但这不是我所期待的,我想要的是:
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;
}
}
}
然后我可以使用此标记作为行号:
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;
}
}
}
}
private void UpdateColors()
{
String[]sStates=新字符串[]{};
如果(m_状态!=null)
{
sStates=m_States.Split(m_-ssparator);
int nState=0;
int nRownNumber=0;
foreach(System.Windows.Forms.DataGridViewRow行中的行)
{
nRownNumber=Convert.ToInt32(row.Tag);
如果(nRownNumber>=0&&nRownNumberm|u couluers|u found|Etats.长度)
{
nState=m_couluers_found_Etats.Length-1;
}
row.DefaultCellStyle.BackColor=m_Couleurs_found_Etats[nState];
row.DefaultCellStyle.ForeColor=m_Couleurs_Texte_Etats[nState];
row.DefaultCellStyle.SelectionBackColor=m_Couleurs_Sel_found_Etats[nState];
row.DefaultCellStyle.SelectionForeColor=m_Couleurs_Sell_Texte_Etats[nState];
nState=0;
}
}
}
}
您可以尝试使用行的标记
属性来放置行索引号。这是我初始化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();
}
我首先循环使用拆分的状态字符串来获取行索引,并将实际值转换为颜色索引。然后,我在行中循环查找放置在标记
属性中的匹配索引属性
以下是仅使用一个循环的已清理版本:
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];
}
}
}
显然需要进行错误检查。非常感谢,这很有效。下次我会记住标签的把戏^^由于tag属性是一个对象,我认为没有任何理由将标记存储为字符串。您可能希望尝试使用“dataGridView1.Rows[i].Tag=i”,然后使用“int rowIndex=i”代替Tostring()和Convert.ToInt版本。从个人角度来说,我更喜欢在行上循环,并通过当前的行标记访问我的状态(如果可能)。这将防止您在每个状态的所有行上循环。但也许我遗漏了什么,我是C#的新手。@Rifu是的,我最初的答案确实有那个不必要的循环。
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;
}
}
}
}
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();
}
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];
}
}
}
}
}
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];
}
}
}