당신이 말한 것 같이
program stop whenever i press the blank row
다음 당신이있어
id = Convert.ToInt32(dataGridView1.SelectedCells[0].Value); //This is the line with the error
Int
가 값 형식이고 null이 될 수 없으며, 때문에 항상 실패 할 때 ' 비어있는 행이 있는데이 셀의 값은 대체로 null
입니다. 이 같은
시도 뭔가 : 셀 값이있는 경우
try
{
id = Convert.ToInt32(dataGridView1.SelectedCells[0].Value); //This is the line with the error
textBox1.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[4].Value.ToString();
}
catch(InvalidCastException exception)
{
// not much to do with it, you can display assign empty valuest to your text boxes or notify user that there's nothing to select
}
대체 apporach는 확인 할 수있다.
if(string.IsNullOrWhiteSpace(dataGridView1.SelectedCells[0].Value))
{
return;
}
id = Convert.ToInt32(dataGridView1.SelectedCells[0].Value);
//Assign values to textboxes
그리고 또 다른 가능성도있다 :
int id;
if(int.TryParse(dataGridView1.SelectedCells[0].Value.ToString(), out id))
{
//Assign values to textboxes
}
당신이있는 DataGridView를 클릭 할 때 응용 프로그램을 시작하거나 할 때 발생하는 문제는? 셀이 숫자가 아니거나, 널이거나, 선택된 행이 없기 때문입니다. null 일 때 대소 문자를 처리하기 위해 "int? id"를 시도 할 수 있습니다. 폼을 생성하는 동안 메서드에 들어갈 수 있으며 datatGridView1.Rows.Count <= 0; – jdweng
'DataGridView'의 빈 행을 클릭 할 때마다 오류가 나타납니다. 아마도 마지막 행이 있습니다. 'id' 변수는'int' 자료형을 가지고 있습니다. @jdweng – StarBoy
변수 형 int를 만들 수 있다고 말했습니까? 그래서 Convert.ToInt32는 오류를주지 않거나 doe dataGridView1.SelectedCells == null 또는 dataGridView1.SelectedCells [0] == null – jdweng