gridview를 내보낼 때 마지막 데이터가 누락되었습니다. 여기 내 데이터를 내보내는 코드가 있습니다.gridview를 내보낼 때 마지막 데이터가 누락되었습니다.
private void ToCsV(DataGridView dGV, string filename)
{
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
for (int i = 0; i < dGV.RowCount - 1; i++)
{
string stLine = "";
for (int j = 1; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
이것은 버튼 코드입니다.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (.xls)|*.xls";
sfd.FileName = "";
if (sfd.ShowDialog() == DialogResult.OK)
{
//ToCsV(dataGridView1, @"c:\export.xls");
ToCsV(gvHistory, sfd.FileName); // Here dataGridview1 is your grid view name
}
나쁜 생각해야한다. 둘째,'Stream'과'.Flush'에 쓰는 것처럼,'StringBuilder'를 비우는 것을 잊지 마십시오. 셋째,'RowCount'가 잘못되었으므로'foreach'를 사용하여 카운터를 선택하지 않아도됩니다 ('foreach'도 빠름). 그리고 if (DataGridViewRow.IsNewRow)'를 테스트 할 수 있습니다. 당신의'DataGridView'에있는 blnak entry row –
(int i = 0; i <** dGV.RowCount - 1 **; i ++)은 어떨까요? 그것은 (int i = 0; i <** dGV.RowCount **; i ++) –