当我将数据导出到 excel 时,代码在一定程度上可以正常工作,但是如果我返回应用程序并在再次导出数据时添加、删除或更新任何数据网格,则不会导出更改。我已经删除了原始的 csv 文件,以防它是一个覆盖问题,但仍然遇到同样的问题。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class FormAccounts : Form
{
String constring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kenny\Documents\Visual Studio 2010\Projects\Cegees 181013\WindowsFormsApplication1\WindowsFormsApplication1\Accounts.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
//String cmdselect = @"select * Accounts";
String cmdupdate = @"update Accounts set date = @date, moneyin = @moneyin, retailin = @retailin, rent = @rent, stock = @stock, transport = @transport, misc = @misc, bills = @bills, comments = @comments where ID = @id";
String cmdinsert = @"insert into Accounts (date, moneyin, retailin, rent, stock, transport, misc, bills, comments) values (@date, @moneyin, @retailin, @rent, @stock, @transport, @misc, @bills, @comments )";
String cmddelete = @"delete from Accounts where ID =@ID";
public FormAccounts()
{
InitializeComponent();
}
private void FormAccounts_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'accountsDataSet.Accounts' table. You can move, or remove it, as needed.
this.accountsTableAdapter.Fill(this.accountsDataSet.Accounts);
}
private void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand(cmdinsert, con);
try
{
da.InsertCommand.Parameters.Add("@date", SqlDbType.Date);
da.InsertCommand.Parameters["@date"].Value = dtpaccs.Value;
da.InsertCommand.Parameters.Add("@moneyin", SqlDbType.Decimal);
da.InsertCommand.Parameters["@moneyin"].Value = textmoneyin.Text ;
da.InsertCommand.Parameters.Add("@retailin", SqlDbType.Decimal);
da.InsertCommand.Parameters["@retailin"].Value = textretailin.Text;
da.InsertCommand.Parameters.Add("@rent", SqlDbType.Decimal);
da.InsertCommand.Parameters["@rent"].Value = textrent.Text;
da.InsertCommand.Parameters.Add("@stock", SqlDbType.Decimal);
da.InsertCommand.Parameters["@stock"].Value = textstock.Text;
da.InsertCommand.Parameters.Add("@transport", SqlDbType.Decimal);
da.InsertCommand.Parameters["@transport"].Value = texttransport.Text;
da.InsertCommand.Parameters.Add("@misc", SqlDbType.Decimal);
da.InsertCommand.Parameters["@misc"].Value = textmisc.Text;
da.InsertCommand.Parameters.Add("@bills", SqlDbType.Decimal);
da.InsertCommand.Parameters["@bills"].Value = textbills.Text;
da.InsertCommand.Parameters.Add("@comments", SqlDbType.VarChar);
da.InsertCommand.Parameters["@comments"].Value = textcomments.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Data Added");
con.Close();
cleartexts();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string date = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
lbldate.Text = date;
lblID.Text = id;
dtpaccs.Value = Convert.ToDateTime(date);
textmoneyin.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textretailin.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textrent.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
textstock.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
texttransport.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
textmisc.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
textbills.Text = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();
textcomments.Text = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter da = new SqlDataAdapter();
da.UpdateCommand = new SqlCommand(cmdupdate, con);
try
{
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = lblID.Text;
da.UpdateCommand.Parameters.Add("@date", SqlDbType.Date).Value = lbldate.Text;
da.UpdateCommand.Parameters.Add("@moneyin", SqlDbType.Decimal).Value = textmoneyin.Text;
da.UpdateCommand.Parameters.Add("@retailin", SqlDbType.Decimal).Value = textretailin.Text;
da.UpdateCommand.Parameters.Add("@rent", SqlDbType.Decimal).Value = textrent.Text;
da.UpdateCommand.Parameters.Add("@stock", SqlDbType.Decimal).Value = textstock.Text;
da.UpdateCommand.Parameters.Add("@transport", SqlDbType.Decimal).Value = texttransport.Text;
da.UpdateCommand.Parameters.Add("@misc", SqlDbType.Decimal).Value = textmisc.Text;
da.UpdateCommand.Parameters.Add("@bills", SqlDbType.Decimal).Value = textbills.Text;
da.UpdateCommand.Parameters.Add("@comments", SqlDbType.VarChar).Value = textcomments.Text;
con.Open();
da.UpdateCommand.ExecuteNonQuery();
MessageBox.Show("Accounts Updated");
con.Close();
cleartexts();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
var rindex = dataGridView1.SelectedCells[0].RowIndex;
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter da = new SqlDataAdapter();
da.DeleteCommand = new SqlCommand(cmddelete, con);
try
{
DialogResult dr;
dr = MessageBox.Show("Are you sure you want to delete this record", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
da.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int).Value = accountsDataSet.Accounts[rindex].ID;
con.Open();
da.DeleteCommand.ExecuteNonQuery();
MessageBox.Show("Record Deleted");
con.Close();
cleartexts();
}
else
{
MessageBox.Show("Delete Cancelled");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void cleartexts()
{
//Clears textboxes
textmoneyin.Text = "";
textretailin.Text ="";
textrent.Text = "";
textstock.Text = "";
texttransport.Text = "";
textmisc.Text = "";
textbills.Text = "";
textcomments.Text = "";
}
private void exportToolStripMenuItem_Click(object sender, EventArgs e)
{
int cols;
string directory = @"C:\Users\kenny\Documents\Visual Studio 2010\Projects\Cegees 181013\WindowsFormsApplication1\WindowsFormsApplication1\Excel Exports";
string filename = string.Format("{0:dd-MM-yy}__{1}.csv", DateTime.Now, "Accounts");
string path = Path.Combine(directory, filename);
//open file
using (StreamWriter wr = File.CreateText(path))
{
//determine the number of cols and write to file
cols = dataGridView1.Columns.Count;
for (int i = 0; i < cols; i++)
{
wr.Write(dataGridView1.Columns[i].Name.ToString().ToUpper() + ",");
}
wr.WriteLine();
//write rows to excel
for (int i = 0; i < (dataGridView1.Rows.Count - 1); i++)
{
for (int j = 0; j < cols; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
wr.Write(dataGridView1.Rows[i].Cells[j].Value + ",");
}
else
{
wr.Write(",");
}
}
wr.WriteLine();
}
wr.Close();
MessageBox.Show("Operation Complete " +path);
}
}
}
}