Tôi có một DataGridView màu sắc các hàng của nó khi thuộc tính Kỳ của nó được đặt.
Hoa là một chuỗi đại diện cho danh sách số được phân cách bằng dấu chấm phẩy.Truy cập DataGridXem hàng thứ tự đã được thêm
Nếu tôi nhận được "0;1;2"
ba hàng đầu tiên sẽ được tô màu tương ứng là màu xanh lá cây, xanh lá cây và đỏ.
Sự cố xảy ra khi tôi sắp xếp dữ liệu nhấp vào tiêu đề cột: màu được áp dụng theo cùng một cách.
Ví dụ:
Names|Labels
Name1|Label1
Name2|Label2
Name3|Label3
tôi nhận "0;1;2"
có nghĩa "Purple;Green;Red"
:
Names|Labels
Name1|Label1 => Purple
Name2|Label2 => Green
Name3|Label3 => Red
tôi loại (giảm dần):
Names|Labels
Name3|Label3 => Red
Name2|Label2 => Green
Name1|Label1 => Purple
tôi nhận "3;4;5"
có nghĩa "Yellow;Orange;Pink"
:
Names|Labels
Name3|Label3 => Yellow
Name2|Label2 => Orange
Name1|Label1 => Pink
Nhưng đây không phải những gì tôi đã chờ đợi là, tôi muốn rằng:
Names|Labels
Name3|Label3 => Pink
Name2|Label2 => Orange
Name1|Label1 => Yellow
Đây là mã của tôi:
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;
}
}
}
không có phải là một cách tiếp cận để sắp xếp thứ tự chúng đã được thêm vào trong DataGridView?
PS: Lần đầu tiên tôi sử dụng hàng.Index thay vì nRowNumber, vì vậy tôi tin rằng vấn đề xuất phát từ đó, nhưng rõ ràng, bộ sưu tập hàng được tổ chức lại hoặc foreach phân tích cú pháp theo rowIndexes.
===== Đây là giải pháp tôi sử dụng nhờ vào LarsTech's answer =====
Sau khi thêm hàng của tôi, tôi gắn thẻ họ theo cách này:
foreach(System.Windows.Forms.DataGridViewRow row in Rows)
{
row.Tag = row.Index;
}
Sau đó, tôi có thể sử dụng này dưới dạng số hàng:
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;
}
}
}
}
Cảm ơn bạn rất nhiều, hoạt động. Tôi sẽ nhớ lần lừa Tag lần sau! ^^ – Rifu
Vì thuộc tính thẻ là một đối tượng, tôi không nghĩ rằng có bất kỳ lý do nào để lưu trữ thẻ dưới dạng chuỗi. bạn có thể muốn thử sử dụng "dataGridView1.Rows [i] .Tag = i" và sau đó "int rowIndex = i" thay cho các phiên bản Tostring() và Convert.ToInt. –
@BPete True. Đã cập nhật. – LarsTech