0
當我將數據導出爲excel時,代碼在某種程度上工作正常,但如果我重新導入應用程序並在我再次導出數據時添加,刪除或更新任何datagrid,不導出更改。我已經刪除了原來的csv文件,以防覆蓋問題,但仍然遇到同樣的問題。將數據導出爲excel
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 [email protected]";
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);
}
}
}
}
你在哪裏將DataGridView綁定到數據源?我注意到你從GridView導出,但是在插入/更新/刪除操作後,我看不到更新綁定。 – SpaceghostAli