简体   繁体   English

更新datagridview后导出日志文件

[英]Exporting log file when datagridview has been updated

I have a datagridview in C# that is used to read data from a SQL database. 我在C#中有一个datagridview,用于从SQL数据库读取数据。 I have it setup so when there is missing criteria, the user can update the datagrid which then updates my SQL database. 我进行了设置,因此当缺少条件时,用户可以更新数据网格,然后更新我的SQL数据库。 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. 但是,我想要/需要生成一个日志文件,并将其与datagrid中更改的列或字段一起导出到本地计算机。 Have some idea of maybe adding an event handler on the datagrid and when cellvaluechanged = true; 有一些想法,也许在datagrid上添加一个事件处理程序,并且当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#)). (没有提供代码,不确定如何使用这种方法(对于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. 您可以在进行更改之前获取DataTable,在进行新更改之后获取dataTable,然后执行此操作。

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. 然后遍历并将这些值写入A.Getchanges()返回的日志中。 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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