保存对话框打开两次保存文件 [英] Save dialogbox open a 2 times to save file

查看:89
本文介绍了保存对话框打开两次保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发人员,

我正在将datagridveiw导出为csv文件,但是当我单击保存"按钮以保存文件时,保存"对话框将打开以次以csv文件格式保存文件.

请检查此代码,并帮助我以正确的格式编写它们:

Hi developers,

I am exporting datagridveiw in csv file, but when I click a save button to save file, the save dialogbox open to times to save file in csv file format .

Please check this code and please help me write them in correct format:

public void DataExport()
      {
          try
          {

              string strColumn = string.Empty;
              string strRow = string.Empty;
              StringBuilder objSB = new StringBuilder();

              for (int i = 0; i < DGV.Columns.Count; i++)
              {
                  strColumn += (i >= DGV.Columns.Count - 1) ? DGV.Columns[i].Name : DGV.Columns[i].Name + ",";
              }

              objSB.AppendLine(strColumn);

              for (int i = 1; i < DGV.Rows.Count - 1; i++)
              {
                  for (int j = 0; j < DGV.Columns.Count; j++)
                  {
                      strRow += (j >= DGV.Columns.Count - 1) ? DGV.Rows[i].Cells[j].Value.ToString().Replace("\n","";) : DGV.Rows[i].Cells[j].Value.ToString().Replace("\n","") + ",";
                  }
                  objSB.AppendLine(strRow);
                  strRow = string.Empty;
              }

              File.AppendAllText(Locations.Text, objSB.ToString());
              DGV.Refresh();
              MessageBox.Show("File Created.");
          }
          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 (SaveDialogbox.ShowDialog()==DialogResult.OK)
              {
                  //Locations.Text = SaveDialogbox.FileName;
                  DataExport();
                  Application.DoEvents();
              }     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 (SaveDialogbox.ShowDialog()==DialogResult.OK)
              {
                  //Locations.Text = SaveDialogbox.FileName;
                  DataExport();
                  Application.DoEvents();
              }
else 
{
messagebox.show("save process failed");
}
}
           catch (System.Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }

推荐答案

当然,对话框会打开两次,您在save_Click方法中有重复的代码:rolleyes:确定要发布吗?正确吗?
Of course the dialog is opening twice, you have duplicate code in the save_Click method :rolleyes: Are you sure you''ve posted it correctly?


Ya Mark已经检测到问题在发布代码时您所做的与粘贴重复代码有关.

这是您在try catch中使用的代码,而问题出在此代码中的是您两次调用ShowDialog()方法,这就是为什么它两次出现的原因.
Ya Mark has already detected Problem what you have did while you posting your code which is about pasting duplicate code.

and this is code you use in try catch and the problem is in this code is you invoke ShowDialog() method two times thats why it appear for two times.

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 (SaveDialogbox.ShowDialog()==DialogResult.OK)
          {
              //Locations.Text = SaveDialogbox.FileName;
              DataExport();
              Application.DoEvents();
          }


删除这个


Remove this one

DialogResult Dialogresult1 = SaveDialogbox.ShowDialog();


这篇关于保存对话框打开两次保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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