简体   繁体   中英

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

How to Store Multiple rows from GridView to Access Database. I have code to store Single Focused row but i need to Store Multiple Rows ? How to Complete My Task ?

this code I used to store single focused row

 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();

This code work for Single Row but i need to store Multiple Rows, Please Help me.

Thanks in Advance.

To obtain all the selected rows, use the GridView GetSelectedRows method. Go through the documentation to know more about this feature.

Here is the way that that you can follow. You can get the multiple selected rows from the xtragrid as below.

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

Here is an example that let you to do Multiple selection using checkbox (web style)

References:
How to select rows via an unbound checkbox column
How to get multiple selected rows in Grid Control
Get values from selected rows from XtraGrid with multiple select and grouping?

Edit: By Comment

To traverse through gridview rows you have to see below topic, which clear you everything that you need..
Traversing Rows
Locating Rows

Example code snippet

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(); }

Reference:
Iterating over all rows in the grid (not the data source) and retrieving values only for the visible rows
Iterate through Grid rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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