2010-02-26 4 views
0

PictureBox로 전달할 이미지 경로를 작성하기 위해 DataGridview에서 행의 값을 사용하고 있습니다. 내가 다른 컨트롤을 사용) (A DataSet.clear을 수행 할 때DataSet.clear()에서 NullreferenceException 문제가 발생했습니다.

private void dataGridView1_SelectionChanged(object sender, EventArgs e) 
{ 
    DataGridViewRow currentRow = new DataGridViewRow(); 
    currentRow = dataGridView1.CurrentRow; 
    string valueJob = currentRow.Cells[2].Value.ToString(); 
    string valueBatch = currentRow.Cells[3].Value.ToString(); 
    string valueImmagine = currentRow.Cells[4].Value.ToString(); 
    string result = Octopus2.Properties.Settings.Default.DataPath + "\\" + valueJob + "\\" + valueBatch + "\\" + valueImmagine + ".jpg"; 
    pictbox.ImageLocation = result; 
} 

문제는 코드는 다음과 같은 오류를 반환한다 : "NullReferenceException이 사용자 코드에 의해 처리되지 않은되었다".

감사합니다. 도움을 주시면 감사하겠습니다.

+0

이 코드는 Exception 또는 Clear()를 발생 시키나요? –

+0

"DataSet.clear()"와 코드 사이의 링크는 무엇입니까? –

+0

[NullReferenceException은 무엇이며 어떻게 수정합니까?] 가능한 복제본 (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine

답변

1

표시 한 코드에서 DataSet.clear()를 표시 할 수 없지만 DataSet.clear()가이 오류를 제공하면 DataSet이 null입니다.

1

앞서 언급했듯이 DataSet.clear()는 컨트롤, 리셋 버튼에 의해 호출되는 다른 이벤트 핸들러에 의해 호출됩니다. 나는이 작업을 수행하여 문제를 해결 :

private void dataGridView1_SelectionChanged(object sender, EventArgs e) 
{ 
    if (dataGridView1.RowCount >=1) 
    { 
     DataGridViewRow currentRow = new DataGridViewRow(); 
     currentRow = dataGridView1.CurrentRow; 
     string valueJob = currentRow.Cells[2].Value.ToString(); 
     string valueBatch = currentRow.Cells[3].Value.ToString(); 
     string valueImmagine = currentRow.Cells[4].Value.ToString(); 

     string result = Octopus2.Properties.Settings.Default.DataPath + "\\" + valueJob + "\\" + valueBatch + "\\" + valueImmagine + ".jpg"; 

     pictboxImpegnativa.ImageLocation = result; 
    } 
    else 
    { 
     MessageBox.Show("good work."); 
    } 
} 

당신의 시간과 도움을 주셔서 감사합니다.

0

currentRow.Cells[x].Value도 null 일 수 있는지 확인하는 것을 잊었습니다. 당신은 다음과 같습니다 다른 코드를 추가해야합니다

if (currentRow.Cells[n].Value != null) 
{ 
    // your code here 
} 

신뢰 나에게, 당신은 널 (null) DataGridViewRow에서뿐만 아니라 하나 잡을 필요가 내부 목적이있다.

관련 문제