c#

[c#] DataGridView

초코렛과자 2016. 2. 11. 23:31

일을 하다보니 c#을 하게되어 요즘 걸음마 수준으로 c#을 해나가고있다. 간단하게 UI를 구성 할 일이 있어 dataGridView를 사용해 봤는데, 생각보다 좋다. dataGridView에 buttonColumn을 추가하여 사용 하는 방법이다.

이를 응용하면 수정이나 삭제도 가능.

출처 : http://purestarman.tistory.com/m/post/248



private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int intchange;
    
    try
    {
        if (dataGridView1.CurrentCell != null)
        {
            // 모든 행의 CheckBox값을 False로 설정
            for (int i = 0; i < dataGridView1.RowCount; i++) 
            {
                dataGridView1[6, i].Value = false; // 6은 CheckBox가 있는 열 번호
            }
            // 현재 선택된 행의 CheckBox값을 True로 설정
            for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) 
            {
                //Convert.ToInt32 => string을 int로 변환
                intchange = Convert.ToInt32(dataGridView1.SelectedRows[i].Cells[0].Value.ToString());
                dataGridView1[6, intchange].Value = true;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}