我使用C#Windows應用程序,從數據庫中獲得並顯示在datagridview的數據和導出到文本文件 我想刪除低於4列之間的空白空間列之間的空間。刪除出口到文本文件
2
vehicle control services ltd
Brom
Malkit
我得到2 vehicle control services ltd brom mal
,但我希望它像2vehicle control services ltdBromMlkit
這是我的代碼。
string stringSql = " SELECT distinct " +
"'" + comboBox6.Text + "' as RecordType" +
" , left([Claimant Name] +' ',30) " +
" , left([Claimant Address1] +' ',30) " +
" , left([Claimant Address2] +' ',30) as ClaimantAddress2 " +
" , left([Claimant Address3] +' ',30) as
導出到文本文件代碼
if (obj == null || obj == Convert.DBNull)
return "";
// if string has no ','
if (obj.ToString().IndexOf(",") == -1)
return obj.ToString();
// remove backslahes
return "\"" + obj.ToString() + "\"";
}
private void ExportDatatviewToCsv(string iFilename, DataView dv)
{
// Open output stream
StreamWriter swFile = new StreamWriter(iFilename);
// Rows of Data
foreach (DataRowView rowData in dv)
{
string[] colData = new string[dv.Table.Columns.Count];
for (int i = 0; i < dv.Table.Columns.Count; i++)
{
object obj = rowData[i];
colData[i] = GetWriteableValueForCsv(obj);
}
// Write data in row
swFile.WriteLine(string.Join(" ", colData));
}
// Close output stream
swFile.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (myDataset == null)
{
return;
}
if (myDataset.Tables[0].Rows.Count == 0)
{
return;
}
DataView vwExport = new DataView(myDataset.Tables[0]);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "TXT file|*.txt";
sfd.FileName = "ee " + ".txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
if (sfd.FileName != "")
{
ExportDatatviewToCsv(sfd.FileName, vwExport);
MessageBox.Show("File has been saved as: " + Environment.NewLine + sfd.FileName + Environment.NewLine + Environment.NewLine + "NB: This dataset has been ordered by t_reference in ascending order. If being combined with an existing dataset - that dataset will also need to be sorted in this way.", "Operation complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
該代碼不是原因,你的SQL部分工作正常。你用什麼代碼從datagridview導出到文本文件? – musefan