DataGridView
에서 셀 색상을 변경하려고합니다.프로그래밍 방식으로 DataGridview의 색상 셀을 변경하십시오.
저는 datagridview에 조금 익숙하며 많은 질문을 여기에 게시했지만 아무 것도 저에게 효과가없는 것으로 보입니다.
중복으로 표시하지 마십시오.
내 마지막 반복 (여전히 작동하지 않습니다)입니다 :
DataGridView1.AutoGenerateColumns = true;
DataGridView1.AutoSize = true;
DataGridView1.DataSource = DT;
int rowIndex = 0;
int cellIndex = 0;
Color c = Color.Gray;
foreach(var row in DataGridView1.Rows.Cast<DataGridViewRow>().ToList())//.ForEach(row =>
{
var cells = row.Cells;
if (cells.Count > 0) {
cellIndex = 0;
foreach (DataGridViewCell cell in cells)
{
DataGridView1.Rows[rowIndex].Cells[cellIndex].Style.BackColor = c;
//cell.Style.BackColor = c;
cellIndex++;
}
if (cells[0].Value!= null)
if ((cells[0].Value as string).Contains("==="))/*end of section*/
{
c = (c == Color.Gray) ? Color.Transparent : Color.Gray;/*change color at end of section*/
}
}
rowIndex++;
}
rowIndex = 0;
어떻게 내 코드에 문제가 될 수 있습니까? 색상을 변경하려면 True \ False로 설정해야하는 매개 변수가 있습니까?
편집 나는 무엇을 : 만 DataGridView
에 이렇게 데이터가 모든 다른 용도가 컨텍스트 메뉴 (MouseButtons.Right
)
더 코드
public partial class ResultsDiffForm : Form
{
public ResultsDiffForm(DataTable DT)//consatructor
{
InitializeComponent();
/*Old code goes here ... */
}
}
와 것을 새로운 양식을 열려면
내가 뭘하는지 명확하게해야합니다
당신이 ("값을" "스타일")'Styles.Add를 사용하여 시도있다;'? –
@DrewKennedy 시도해 보겠습니다 – LordTitiKaka
명확히하기 위해 CSS 규칙을 사용합니다 :'Styles.Add ("background-color", "gray");' –