![](/img/trans.png)
[英]Set focus to a row in datagridview that has been newly inserted or updated?
[英]Exporting log file when datagridview has been updated
我在C#中有一個datagridview,用於從SQL數據庫讀取數據。 我進行了設置,因此當缺少條件時,用戶可以更新數據網格,然后更新我的SQL數據庫。 但是,我想要/需要生成一個日志文件,並將其與datagrid中更改的列或字段一起導出到本地計算機。 有一些想法,也許在datagrid上添加一個事件處理程序,並且當cellvaluechanged = true時; 運行出口? 感謝您的任何幫助,謝謝!
(沒有提供代碼,不確定如何使用這種方法(對於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);
您可以在進行更改之前獲取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
然后遍歷並將這些值寫入A.Getchanges()返回的日志中。 這些就是所做的更改。
我知道了:
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.