簡體   English   中英

datagridview更新行到csv文件

[英]datagridview update row to csv file

我正在為庫存應用程序在C#項目中使用Csv文件和datagridview,我嘗試將一行更新為CSV文件!

如果用戶用新單詞編輯行當前單詞 ,我需要更新,但是我的問題是我需要保存當前單詞和新單詞,並在偽代碼示例中獲取總計:

foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if(row in column is modified)
                    update specific row with comma to current file and load it...
            }

CSV文件看起來像,當前:

1; 2; 4; 5

更新:

1; 2,A ;; 4; 5更改設備A總計:1次...

下一行已修改:

1; A ;; 4BC; 5更改了設備BC的總更改:2次...

有了數據庫,就很容易更新數據,但是我沒有安裝sql server,所以我認為這個選項對我來說不是。

我的目標是跟蹤設備的進/出,因此,如果您有解決方案,請與我們分享。

如果不使用SQL Server,也許這樣的事情可能會有所幫助 LiteDB您將擁有LiteDB來托管數據,並在需要時將其導出為CSV。 使用CSV文件通常意味着您每次要進行更新時都會重寫整個文件……這既緩慢又麻煩。 我建議您使用CSV將數據從點A傳輸到點B,但不維護數據。

另外,如果您真的想堅持使用CSV,請查看以前稱為JET驅動程序的Microsoft Ace OLEDB驅動程序。 我用它來查詢CSV文件,但是我從未用它來更新...所以您的里程可能會有所不同。

除了使用實際的數據庫或數據庫驅動程序外,您還必須將StreamReader和StreamWriter一起使用。 使用StreamReader讀取文件,使用StreamWriter寫入新文件。 在您的StreanReader中。 這意味着您將在StreamReader中包含代碼以找到正確的行進行更新。

這是我創建並用於與LiteDB交互的類。 它並不是那么強大,但是它確實做了我當時需要做的事情。 我必須對平台上托管的大量產品進行更改,並以此跟蹤進度。

using System;
using LiteDB;

namespace FixProductsProperty
{

    public enum ListAction
    {
        Add = 0,
        Remove,
        Update,
        Disable,
        Enable
    }

    class DbInteractions
    {
        public static readonly string dbFilename = "MyDatabaseName.db";
        public static readonly string dbItemsTableName = "MyTableName";
        public void ToDataBase(ListAction incomingAction, TrackingDbEntry dbEntry = null)
        {

            if (dbEntry == null)
            {
                Exception ex = new Exception("dbEntry can not be null");
                throw ex;
            }


            // Open database (or create if not exits)
            using (var db = new LiteDatabase(dbFilename))
            {

                var backupListInDB = db.GetCollection<TrackingDbEntry>(dbItemsTableName);

                //ovverride action if needed
                if (incomingAction == ListAction.Add)
                {
                    var tempone = backupListInDB.FindOne(p => p.ProductID == dbEntry.ProductID);
                    if (backupListInDB.FindOne(p => p.ProductID == dbEntry.ProductID) != null)
                    {
                        //the record already exists
                        incomingAction = ListAction.Update;
                        //IOException ex = new IOException("Err: Duplicate. " + dbEntry.ProductID + " is already in the database.");
                        //throw ex;
                    }
                    else
                    {
                        //the record does not already exist
                        incomingAction = ListAction.Add;
                    }
                }

                switch (incomingAction)
                {
                    case ListAction.Add:
                        backupListInDB.Insert(dbEntry);
                        break;
                    case ListAction.Remove:
                        //backupListInDB.Delete(p => p.FileOrFolderPath == backupItem.FileOrFolderPath);
                        if (dbEntry.ProductID != 0)
                        {
                            backupListInDB.Delete(dbEntry.ProductID);
                        }
                        break;
                    case ListAction.Update:
                        if (dbEntry.ProductID != 0)
                        {
                            backupListInDB.Update(dbEntry.ProductID, dbEntry);
                        }
                        break;
                    case ListAction.Disable:
                        break;
                    case ListAction.Enable:
                        break;
                    default:
                        break;
                }

                backupListInDB.EnsureIndex(p => p.ProductID);
                // Use Linq to query documents
                //var results = backupListInDB.Find(x => x.Name.StartsWith("Jo"));
            }
        }

    }
}

我這樣使用它:

DbInteractions yeah = new DbInteractions();
yeah.ToDataBase(ListAction.Add, new TrackingDbEntry { ProductID = dataBoundItem.ProductID, StoreID = dataBoundItem.StoreID, ChangeStatus = true });

抱歉...我的變量命名約定有時會失效...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM