How To Display Selected Rows From One Datagridview To Another Datagridview In C# Windows Application

How To Display Selected Rows From One DataGridView To Another DataGridView In C# Windows Application

How To Show Selected Rows From One DataGridView To Another DataGridView In Different Forms In C# Windows Application
This Tutorial shows How To Display Selected Rows From One DataGridView To Another DataGridView In Different Forms Using C# Windows Application
This Tutorial shows How To Display Selected Rows From One DataGridView To Another DataGridView In Different Forms Using C# Windows Application

Whats Covered In This Guide:

  1. Check If There Is At Least One Row Selected From The First Datagridview.
  2. Check If Second Datagridview Rows Are Empty, If Not, Clear The Rows.
  3. Populate Second Datagridview With Selected Rows From The First Datagridview.
  4. How To Change Datagridview Row Height.
  5. How To Adjust Datagridview Column To Fit.
  6. How To Start Selected Rows Form In The Center Screen.

This Tutorial shows How To Show Selected Rows From One DataGridView To Another DataGridView In Different Forms In C# Windows Application

C# SOURCE CODE

        private void button9_Click(object sender, EventArgs e)
        {

            //Check if there is a row selected
            if (dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows != null)
            {
                //MessageBox.Show(dataGridView1.SelectedRows.Count + " rows Selected ");
                DisplayAllSelectedRowsOnDataGridView showSelected = new DisplayAllSelectedRowsOnDataGridView();
                //Changing DataGridView Row Height
                showSelected.dataGridView2.RowTemplate.Height = 80;
                //First clear 2nd grid rows if there is more than zero rows
                if (showSelected.dataGridView2.Rows.Count > 0)
                {
                    showSelected.dataGridView2.Rows.Clear();
                }

               
                for (int rowsSelectedIndex = 0; rowsSelectedIndex < dataGridView1.SelectedRows.Count; rowsSelectedIndex++)
                {
                    int index = showSelected.dataGridView2.Rows.Add();
                    showSelected.dataGridView2.Rows[index].Cells[0].Value = dataGridView1.SelectedRows[rowsSelectedIndex].Cells[0].Value.ToString();
                    showSelected.dataGridView2.Rows[index].Cells[1].Value = dataGridView1.SelectedRows[rowsSelectedIndex].Cells[1].Value.ToString();
                    showSelected.dataGridView2.Rows[index].Cells[2].Value = dataGridView1.SelectedRows[rowsSelectedIndex].Cells[2].Value.ToString();
                    showSelected.dataGridView2.Rows[index].Cells[3].Value = dataGridView1.SelectedRows[rowsSelectedIndex].Cells[3].Value.ToString();
                    showSelected.dataGridView2.Rows[index].Cells[4].Value = dataGridView1.SelectedRows[rowsSelectedIndex].Cells[4].Value.ToString();
                    showSelected.dataGridView2.Rows[index].Cells[5].Value = dataGridView1.SelectedRows[rowsSelectedIndex].Cells[5].Value;

                }

                showSelected.ShowDialog();
            }
                //If there is no row selected
            else
            {
                MessageBox.Show("No row Selected ");
            }
        }

Leave a Reply

Your email address will not be published. Required fields are marked *