无法以正确的格式保存来自datagridview的csv文件 [英] Does not save csv file from datagridview in correct format

查看:88
本文介绍了无法以正确的格式保存来自datagridview的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi开发人员的
我正在将一个csv文件加载到数据集中并在datagridview中可视化它们,并对数据进行一些操作后,我又以csv文件格式保存了一个datagridview值.


但是一段时间以来,我的csv文件未以正确的格式保存csv文件,因为该单元格中的值落入了其他单元格中
例如
如果在原始csv文件中,值位于[0]索引位置,则从datagridview保存此文件后,值将位于[3]索引位置.

并给出了一些未知类型的值,我不知道,因为我的原始csv文件无法保留此类型的值

"PASI ID:< 10441494957439>
"
请检查我的代码.

Hi developer''s
I am loading a csv file in dataset and visualize them in datagridview and after applied some operation on data , i am save a datagridview values again in csv file format.


but from some time my csv file not save csv file in correct format because values from the cell are fall to the another cells
for example
if in a orignal csv file values at [0] index location, after save this file from datagridview values fall at the [3] index location .

and given some unknown type of values , I do not know Because my orignal csv file not cantain this type values

"PASI ID : < 10441494957439>
"
please check my code .

  public void DataExport()
        {
            try
            {
                string strColumn = string.Empty;
                string strRow = string.Empty;
                StringBuilder objSB = new StringBuilder();
                for (int i = 0; i < Datagidveiw1.Columns.Count; i++)
                {
                    strColumn += (i >= Datagidveiw1.Columns.Count - 1) ? Datagidveiw1.Columns[i].Name : Datagidveiw1.Columns[i].Name + ",";
                }
                objSB.AppendLine(strColumn);

                for (int i = 1; i < Datagidveiw1.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < Datagidveiw1.Columns.Count; j++)
                    {
                        strRow += (j >= Datagidveiw1.Columns.Count - 1) ? Datagidveiw1.Rows[i].Cells[j].Value.ToString().Replace("\n", "") : Datagidveiw1.Rows[i].Cells[j].Value.ToString().Replace("\n", "") + ",";
                    }
                    objSB.AppendLine(strRow);
                    strRow = string.Empty;
                }
                File.AppendAllText(Locations.Text, objSB.ToString());
                Datagidveiw1.Refresh();
               
                MessageBox.Show("Data save sucessfull");
                Application.Exit();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void save_Click(object sender, EventArgs e)
        {
            
            try
            {
                SaveFileDialog SaveDialogbox = new SaveFileDialog();
                SaveDialogbox.FileName = Locations.Text;
                SaveDialogbox.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
                SaveDialogbox.FilterIndex = 1;
                SaveDialogbox.RestoreDirectory = true;
                DialogResult Dialogresult1 = SaveDialogbox.ShowDialog();
                if (Dialogresult1 == DialogResult.OK)
                {
                    Locations.Text = SaveDialogbox.FileName;
                    DataExport();
                    Application.DoEvents();
                }
                 else
                 {
                 MessageBox.Show("Invalid Arguments");
                }
}}



请检查此代码,
感谢



please check this code,
thanks

推荐答案

您好,Vishu,

快速查看objSB.ToString()并尝试直接以CSV格式保存在记事本中,这样您就可以额外看到那些字符和字符串,需要删除字符串形式.
Hi Vishu,

Get the objSB.ToString()in quick watch and try to save directly in notepad in CSV format so you can able to visible those character and string extra, need to removed form string.


I会略有不同.

为了生成列名,我将使用原始的DataSet.与其使用for循环,不如使用foreach循环,它们的开销要小于for循环

例如.

假设您的数据集称为ds.

I would approach this slightly differently.

To generate the column names, I would use the original DataSet. Instead of using a for loop I would a foreach loop they ahve less over head then the for loops

eg.

let''s assume that your dataset is called ds.

StringBuilder sb = new Stringbuilder();

int numColumns = Tables[0].Columns.Count-1 //-1 is for zero based count
int columnCounter= numColumns;

foreach(DataColumn dc in ds.Tables[0].Columns)
{
    sb.Append(dc.ColumnName);
    AppendDelimiter(sb, ref columnCounter);
}



foreach(GridViewRow gvr in Datagidveiw1.Rows)
{
   columnCounter=numColumns
   foreach(TableCell c in grv.Cells)
   {
       sb.Append(c.Text)
       AppendDelimiter(sb,ref columnCounter);
   }
}


private void AppendDelimiter(StringBuilder sb, ref int columnCounter)
{
       if (columnCounter > 0)
         sb.Append('';'');
       else
         sb.Append(Environment.NewLine);

       columnCounter--;
}


在此处执行其余代码以将内容写入文件.

我已经对此进行了测试,并且效果很好.

如果您还有其他问题,那么我想看看您用来构建gridview的代码,或者是填充的gridview的屏幕截图.

另一种选择是将您在gridview中的更改保留到DataSet,然后遍历DataSet.Table [0] .Rows创建csv.


do the rest of the code here to write content to file.

I have tested this and it worked just fine.

If you have any more issues then I would like to see the code that you use to construct your gridview and or a screenshot of the populated gridview.

Another alternative is to persit your changes in the gridview to the DataSet and then iterate through the DataSet.Table[0].Rows to create the csv.


这篇关于无法以正确的格式保存来自datagridview的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆