How To Show DataGridView Selected Row Data In Another Form In C# Windows Forms Application

How To Show DataGridView Selected Row Data In Another Form On Button Click In C# Windows Forms Application

How To Show DataGridView Selected Row Data In Another Form On Button Click In C# Windows Forms Application
How To Show DataGridView Selected Row Data In Another Form In C# Windows Forms Application
How To Show DataGridView Selected Row Data In Another Form In C# Windows Forms Application

This tutorial shows How To Show DataGridView Selected Row Data In Another Form In C# Windows Forms Application On Button Click

C# SOURCE CODE

        private void button8_Click(object sender, EventArgs e)
        {
            //Check if there is atleast one row selected
            if (dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows != null)
            {
                //Check If More Than One Row Is Selected
                if (dataGridView1.SelectedRows.Count > 1)
                {
                     MessageBox.Show("Multiple Rows Selected. Please Select One row only");
                }
                else if (dataGridView1.SelectedRows.Count == 1)
                {
                    DataGridViewRow selectedDatagridViewRow = dataGridView1.SelectedRows[0]; 
                    //Display Another Form On Button Click
                    ViewSelectedDatagridviewRow showNewFormOnButtonClick = new ViewSelectedDatagridviewRow();
                    showNewFormOnButtonClick.txtBoxFirstName.Text = selectedDatagridViewRow.Cells[0].Value.ToString();
                    showNewFormOnButtonClick.txtBoxLastName.Text = selectedDatagridViewRow.Cells[1].Value.ToString();
                    showNewFormOnButtonClick.txtBoxGender.Text = selectedDatagridViewRow.Cells[2].Value.ToString();
                    showNewFormOnButtonClick.txtBoxCountry.Text = selectedDatagridViewRow.Cells[3].Value.ToString();
                    showNewFormOnButtonClick.txtBoxImagePath.Text = selectedDatagridViewRow.Cells[4].Value.ToString();
                   
                    //Start the form In the center screen
                    //showNewFormOnButtonClick.StartPosition = FormStartPosition.CenterScreen;
                    showNewFormOnButtonClick.ShowDialog();
                    //MessageBox.Show("One row selected");
                }
            }
            else
            {
                MessageBox.Show("No Row Selected");
            }
        }
READ MORE  How To Populate Datagridview With Static Data

Leave a Reply

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