简体   繁体   中英

Exporting log file when datagridview has been updated

I have a datagridview in C# that is used to read data from a SQL database. I have it setup so when there is missing criteria, the user can update the datagrid which then updates my SQL database. However I'm wanting / needing to have a log file generated and exported with what column or field that was changed in the datagrid out to the local machine. Have some idea of maybe adding an event handler on the datagrid and when cellvaluechanged = true; run export? Any help is appreciated, thx!

(No code to provide, not sure how to approach this type of method (still kinda green to C#)).

sqldataAdapter da;
sqlCommandBuilder scb;
DataTable dt;
SQLConnection con = (my connection);
private void btnEnter_Click(object sender, EventArgs e)
    {
        try
        {
         //Searches database for what is plugged into txtbox.   
            da = new SqlDataAdapter("SELECT * FROM [sqltable] WHERE [columnA]='" + txtData.Text + "' OR [ColumnB]='" + txtData.Text + "' OR [ColumnC]='" + txtData.Text + "' OR [ColumnD]='" + txtData.Text + "'", con);
            ds = new DataSet();
            dt = new DataTable();
            ds.Clear();
            da.Fill(dt);
            dg.DataSource = dt;

            con.Open();
            con.Close();
private void btnUpdate_Click(object sender, EventArgs e)
    {
        //when button is clicked, the SQL Database gets updated with the data that is plugged into the datagridview.
        scb = new SqlCommandBuilder(da);
        da.Update(dt);

You can grab the DataTable of before the change was made, and the dataTable after the new changes have been made, then do this.

A.Merge(B); // this will add to A any records that are in B but not A
return A.GetChanges(); // returns records originally only in B

Then go through and write the values to a log that a A.Getchanges() returns. Those would be the changes made.

I got it:

dg.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText;
        dg.SelectAll();

        Clipboard.SetDataObject(dg.GetClipboardContent());
        File.WriteAllText(@"path.txt", Clipboard.GetText(TextDataFormat.Text));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM