I'm a beginner of C# users.
I'm using the DevExpress with C# Window Form Application.
I'm trying to move data from gridcontrol_1 to gridcontrol_2.
The gridcontrol_1 connected from MSSQL DB TableA.
I want to set DataTable same as gridcontrol_1 into gridcontrol_2. (like shipping cart)
If I select items from gridcontrol_1 then move to gridcontrol_2 by click event.
If I deselect items from gridcontrol_2 then move back to gridcontrol_2 by click event.
Finally, I want to save(insert) the gridcontrol_2 items to the SQL server TableB.
I don't know how to code what I want.
I found a video clip from youtube similarly I want to do.
But, the clip shows only DataGridView Control, not GridControl.

Mayooran NavamanyRank #438 · 5.7K reputationPosted Jun 23, 2024, 12:31 AM EDT
Step 1: Set up your GridControls
Assuming you have
gridControl1andgridControl2on your form, and both are connected to their respective data sources.Step 2: Load Data into gridControl1
First, you need to load data from
TableAin your SQL Server database intogridControl1.// Method to load data from TableA private void LoadDataGrid1() { string connectionString = "your_connection_string_here"; using (SqlConnection conn = new SqlConnection(connectionString)) { string query = "SELECT * FROM TableA"; SqlDataAdapter adapter = new SqlDataAdapter(query, conn); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); gridControl1.DataSource = dataTable; } }Step 3: Select Rows and Move them to gridControl2
Next, you need to handle the click event to move selected rows from
gridControl1togridControl2.// Click event to move selected rows from gridControl1 to gridControl2 private void btnMoveToGrid2_Click(object sender, EventArgs e) { DataTable grid2DataTable = gridControl2.DataSource as DataTable; if (grid2DataTable == null) { grid2DataTable = (gridControl1.DataSource as DataTable).Clone(); // Clone structure of gridControl1 DataTable gridControl2.DataSource = grid2DataTable; } int[] selectedRows = gridView1.GetSelectedRows(); foreach (int rowHandle in selectedRows) { DataRow row = gridView1.GetDataRow(rowHandle); grid2DataTable.ImportRow(row); } foreach (int rowHandle in selectedRows.OrderByDescending(v => v)) { gridView1.DeleteRow(rowHandle); } }Step 4: Move Rows Back to gridControl1
Similarly, you need to handle the click event to move rows back from
gridControl2togridControl1.// Click event to move selected rows from gridControl2 to gridControl1 private void btnMoveToGrid1_Click(object sender, EventArgs e) { DataTable grid1DataTable = gridControl1.DataSource as DataTable; int[] selectedRows = gridView2.GetSelectedRows(); foreach (int rowHandle in selectedRows) { DataRow row = gridView2.GetDataRow(rowHandle); grid1DataTable.ImportRow(row); } foreach (int rowHandle in selectedRows.OrderByDescending(v => v)) { gridView2.DeleteRow(rowHandle); } }Step 5: Save Data from gridControl2 to TableB
Finally, you can save the data from
gridControl2toTableBin your SQL Server database.// Method to save data from gridControl2 to TableB private void SaveDataGrid2ToDB() { DataTable grid2DataTable = gridControl2.DataSource as DataTable; if (grid2DataTable == null || grid2DataTable.Rows.Count == 0) { MessageBox.Show("No data to save."); return; } string connectionString = "your_connection_string_here"; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction transaction = conn.BeginTransaction()) { using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction)) { bulkCopy.DestinationTableName = "TableB"; try { bulkCopy.WriteToServer(grid2DataTable); transaction.Commit(); MessageBox.Show("Data saved successfully."); } catch (Exception ex) { transaction.Rollback(); MessageBox.Show($"Error: {ex.Message}"); } } } } }Step 6: Add Event Handlers
Don't forget to wire up your button click events to the appropriate handlers.
public Form1() { InitializeComponent(); LoadDataGrid1(); btnMoveToGrid2.Click += new EventHandler(btnMoveToGrid2_Click); btnMoveToGrid1.Click += new EventHandler(btnMoveToGrid1_Click); btnSaveToDB.Click += new EventHandler(SaveDataGrid2ToDB); }TableAintogridControl1.gridControl1togridControl2.gridControl2togridControl1if needed.gridControl2toTableBin your SQL Server database.By following these steps, you should be able to move data between two DevExpress GridControls and save the data to a SQL Server table.
MohamedEid102 reputationPosted Jun 24, 2024, 1:59 PM EDT
Biggest Thanks for you mister
but i use linq not SqlConnection how to use it with this code