簡體   English   中英

如何將多行從GridView Winform Devexpress存儲到Access數據庫?

[英]How to Store multiple Rows from GridView Winform Devexpress to Access Database?

如何將多行從GridView存儲到Access數據庫。 我有存儲單焦點行的代碼,但我需要存儲多行? 如何完成我的任務?

我用來存儲單個焦點行的代碼

 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Samples.accdb");
            conn.Open();



            string s1 = (string)gridView1.GetFocusedRowCellValue("Item");
            string s2 = (string)gridView1.GetFocusedRowCellValue("Description");
            string s3 = (string)gridView1.GetFocusedRowCellValue("UoM");
            object quant = gridView1.GetFocusedRowCellValue("Quantity");
            object price = gridView1.GetFocusedRowCellValue("Price");
            object taxp = gridView1.GetFocusedRowCellValue("Tax in Percentage");
            object taxa = gridView1.GetFocusedRowCellValue("Tax in Amount");
            object total = gridView1.GetFocusedRowCellValue("Total");


            int a = Convert.ToInt32(quant);
            int b = Convert.ToInt32(price);
            int c = Convert.ToInt32(taxp);
            int d = Convert.ToInt32(taxa);
            int e = Convert.ToInt32(total);


                OleDbCommand cmd = new OleDbCommand("insert into gridview(Item,Description,UoM,Quantity,Price,TaxinPercentage,TaxinAmount,Total) values ('" + s1 + "','" + s2 + "','" + s3 + "', " + a + " ,  " + b + " , " + c + " , " + d + "  , " + e + " )", conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Inserted Successful","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                Close();

此代碼適用於單行,但我需要存儲多行,請幫助我。

提前致謝。

若要獲取所有選定的行,請使用GridView GetSelectedRows方法。 瀏覽文檔以了解有關此功能的更多信息。

這是您可以遵循的方式。 您可以從xtragrid獲取多個選定的行,如下所示。

int[] selRows = ((GridView)gridControl1.MainView).GetSelectedRows();
DataRowView selRow = (DataRowView)(((GridView)gridControl1.MainView).GetRow(selRows[0]));
txtName.Text = selRow["name"].ToString();

這是一個示例,可讓您使用復選框(網絡樣式)進行多項選擇

參考文獻:
如何通過未綁定的復選框列選擇行
如何在網格控件中獲取多個選定的行
從具有多個選擇和分組的XtraGrid中從選定的行中獲取值?

編輯:通過評論

要遍歷gridview行,您必須看到以下主題,該主題清除了您需要的所有內容。
橫行
定位行

示例代碼段

using DevExpress.XtraGrid.Views.Base;

ColumnView View = gridControl1.MainView as ColumnView;
View.BeginUpdate();
try {
   int rowHandle = 0;
   DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["Category"];
   while(true) {
      // locating the next row 
      rowHandle = View.LocateByValue(rowHandle, col, "SPORTS");
      // exiting the loop if no row is found 
      if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
         break;
      // perform specific operations on the row found here 
      // ... 
      rowHandle++;
   }
} finally { View.EndUpdate(); }

參考:
遍歷網格中的所有行(非數據源)並僅檢索可見行的值
遍歷網格行

暫無
暫無

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

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