Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:
C#
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);
           }

       }
Posted
Updated 18-Nov-10 21:29pm
v5
Comments
Dalek Dave 19-Nov-10 3:29am    
Edited for Grammar and Syntax.

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?
 
Share this answer
 
v2
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900