Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从GridView Winform Devexpress存储多行以访问数据库?_C#_Ms Access_Gridview_Devexpress - Fatal编程技术网

C# 如何从GridView Winform Devexpress存储多行以访问数据库?

C# 如何从GridView Winform Devexpress存储多行以访问数据库?,c#,ms-access,gridview,devexpress,C#,Ms Access,Gridview,Devexpress,如何将多行从GridView存储到Access数据库。我有代码来存储单个重点行,但我需要存储多行?如何完成我的任务 这段代码我用来存储单个聚焦行 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Samples.accdb"); conn.Open(); string s1 = (strin

如何将多行从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();
此代码适用于单行,但我需要存储多行,请帮助我


提前感谢。

要获取所有选定行,请使用该方法。浏览文档以了解有关此功能的更多信息

这是你可以遵循的方法。您可以从xtragrid获取多个选定行,如下所示

int[] selRows = ((GridView)gridControl1.MainView).GetSelectedRows();
DataRowView selRow = (DataRowView)(((GridView)gridControl1.MainView).GetRow(selRows[0]));
txtName.Text = selRow["name"].ToString();
下面是一个例子,让您可以这样做

参考文献:


编辑:通过评论

要遍历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(); }
参考资料:


你好,Niranjan,谢谢您的回答,但我需要保存GridView中的所有数据以访问Datbase非选定行。我需要存储所有行。