2013-06-20 3 views
-1

문제는 내가 RowTemplate.DefaultCellStyle.SelectionBackColor에 의해 선택한 행의 색상을 내 양식 생성자에서 변경할 때 작동하지만 사용자가 선택한 그리드를 변경하기 위해 버튼을 클릭 할 때 버튼 이벤트에서 작동하지 않는다는 것입니다. 다시 색상! 도움주세요.datagridview 선택 색상을 동적으로 변경하는 C#

public Form1() 
{ 
    InitializeComponent(); 
    dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Red; //this  works fine 
} 
void button2_Click(object sender, EventArgs e) 
{ 
    dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Blue;//but this does not work 
} 

답변

1

이 시도 ..

void dataGridView1_RowPrePaint(object sender, 
    DataGridViewRowPrePaintEventArgs e) 
{ 
    If (DatagridView1.Rows(DataGridView1.CurrentCell.RowIndex).Selected) 
    { 

    DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).DefaultCellStyle.SelectionBackColor=Color.Blue; 

    } 
} 
+0

있는 EventArgs는 더 rowIndex에 속성이 없습니다 !!!!! –

+0

@IsaacShakiba .. Sorrrryy !! 코드에서 붙여 넣기 오류를 복사하십시오. P .. '업데이트 된 .. 다시 시도하십시오 .. – matzone

+0

임시 색상 선택을 변경하고 싶지 않습니다! –

0
void button2_Click(object sender, EventArgs e) 
{ 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     row.DefaultCellStyle.SelectionBackColor = Color.Blue 
    } 
} 

업데이트 :

void button2_Click(object sender, EventArgs e) 
{ 
    foreach (DataGridViewColumn col in dataGridView1.Columns) 
    { 
     col.DefaultCellStyle.BackColor = Color.Blue 
    } 
} 
+0

이 코드는 기존 행의 선택 색상을 변경합니다. 그리드에 행이 추가되면 선택 색상이 기본 색상으로 변경됩니다! –

+0

답변을 업데이트했습니다. –

관련 문제