텍스트 파일에 쓰려는 DataGridview가 있습니다. 여기 내 코드입니다 :DataGridView에서 Windows Forms의 텍스트 파일로 C#
private void WriteToFile_Click(object sender, EventArgs e)
{
StreamWriter sW = new StreamWriter("list.txt");
for (int i = 0; i < 6; i++)
{
string lines = "";
for (int col = 0; col < 6; col++)
{
lines += (string.IsNullOrEmpty(lines) ? " " : ", ") +
dataGridView.Rows[i].Cells[col].Value.ToString();
}
sW.WriteLine(lines);
sW.Close();
}
}
내가 버튼을 클릭 할 때 나에게 오류 제공 :
System.NullReferenceException
안녕 조를 위해
첫 번째 루프및
를 사용하려면, 귀하의 질문에 좀 더 노력을 넣어보십시오. 예를 들어 WriteToFile_Click을 통해 디버깅 할 때 null 참조가 반환되는 위치는 어디입니까? 이와 비슷한 세부 정보는 도움이됩니다. – Alex격자가 6x6보다 작지 않은지 확인하십시오. – wdc
오 죄송합니다. + = (string.IsNullOrEmpty (lines)? "": ",") + dataGridView.Rows [i] .Cells [ col] .Value.ToString(); –