I have a form form1 and there is a search button for searching the doctor when i click search button then a new form opens i.e. form2 and in form2 there is a data grid view control and i want when i select any doctor and click on apply button then this doctor id and name comes onto the form1 textbox1 and textbox2.

This is for using C# window application and i am using sql database.

Check out this code:

//form1:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
        }

        public void GetDataFromForm2(List<object> list)
        {
            textBox1.Text = list[0].ToString();
            textBox2.Text = list[1].ToString();
        }
    }

//form2:
public partial class Form2 : Form
    {
        Form1 f1;
        List<Doctor> list;
        public Form2(Form1 _f1)
        {
            InitializeComponent();          
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            this.f1 = _f1;

            //populate DGV:
            PopulateDoctors();
        }

        private void PopulateDoctors()
        {
            list = new List<Doctor>
            {
                new Doctor{ID=1, Name="Doctor 1"},
                new Doctor{ID=2, Name="Doctor 2"}
            };
            dataGridView1.DataSource = list;
        }

        private void dataGridView1_CellClick(object obj, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;
            List<object> data = new List<object>();
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                if (rowindex == row.Index)
                {                   
                    foreach (DataGridViewCell cell in row.Cells)
                        data.Add(cell.Value.ToString());
                    break;
                }
            }
            //send data to form1:
            f1.GetDataFromForm2(data);
            //close this form2:
            this.Dispose();
        }
    }

    class Doctor
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

Mitja

i have use this code but there is a error....

Expected ; or = (cannot specify constructor arguments in declaration)

this is at line

f1.GetDataFrom DoctorGridView(data);
            this.Dispose();

Edited 5 Years Ago by vivekagrawal: n/a

now the error is Object reference not set to an instance of an object. at line

data.Add(cell.Value.ToString());

Edited 5 Years Ago by vivekagrawal: n/a

Code works fine. I have tested it (becuase I made it by my self).

btw, could you just copy-past the code to your clean project? The only think that C# generates its self is th button click (to open form2).
This one you have to put it on the form and add an event of the Click (and paste the code from my button_click to yours). All other code you just copy-paste.

It has to work.
And btw, where did you find this like of code in mine:

f1.GetDataFromDoctorGridView(data);

My code only has:

f1.GetDataFromForm2(data);

plz deals with the new error of
Object reference not set to an instance of an object. at line

mitja i am using databinding for datagrid view and ur code is giving id=1 and name= doctor1
i want when i click data grid view cell then id and name shows in form1 text box but there was error.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hospital_point
{
    public partial class DoctorGridView : Form
    {
        PatientFile f1;
        //List<Doctor> list;
        public DoctorGridView(PatientFile _f1)
        {
            InitializeComponent();
            this.dgvRefferedDoctor.CellClick += new DataGridViewCellEventHandler(dgvRefferedDoctor_CellClick);
            this.f1 = _f1
}
  private void DoctorGridView_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hOSPITAL_POINTDataSet.DOCTOR_DETAILS' table. You can move, or remove it, as needed.
            this.dOCTOR_DETAILSTableAdapter.Fill(this.hOSPITAL_POINTDataSet.DOCTOR_DETAILS);

        }
 class Doctor
        {
            public int DOCTOR_ID { get; set; }
            public string DOCTOR_NAME { get; set; }
        }

        private void dgvRefferedDoctor_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;
            List<object> data=new List<object>();
            foreach(DataGridViewRow row in this.dgvRefferedDoctor.Rows)
            {
                if(rowindex==row.Index)
                {
                    foreach(DataGridViewCell cell in row.Cells)
                        data.Add(cell.Value.ToString());
                    break;
                }
            }
            f1.GetDataFromDoctorGridView(data);
            this.Dispose();
        }
    }
}

This is form 2 code whose name is DoctorGridView.

And this is the method you have it on Form1:

etDataFromDoctorGridView

right?

So where is the problem? I dont see it, at least if you populate the dataGridView.

How many columns do you have in the dgv? Please, put a break point on dgv_CellClick event, to see whats going on.

Edited 5 Years Ago by Mitja Bonca: n/a

IN form1 there showing index is out of range exception

public void GetDataFromDoctorGridView(List<object> list)
        {
            
            txtdoctorid.Text = list[0].ToString();
            txtdoctorname.Text = list[1].ToString();

        }

Does the list get filled up?

I asked you how many columns does your DGV have?
my code was made for 2 columns, so if you dont have two (less or more) might be a problem.
You have to get two values into a List<object>, those from two columns (id and name).

Here is the colution:

private void dataGridView1_CellClick(object obj, DataGridViewCellEventArgs e)
        {
             int rowindex = e.RowIndex;
            List<object> data = new List<object>();
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                if (rowindex == row.Index)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        //SET INDEXES OF THOSE 2 COLUMNS WHERE YOU HAVE ID AND NAME
                        //my example code shows 1st and 2nd column (indexes of 0 and 1):
                        if (e.ColumnIndex == 0 && e.ColumnIndex == 1) 
                            data.Add(cell.Value.ToString());
                    }
                    break;
                }
            }
        }

again the error is coming on click a cell of grid view "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:Index"

public void GetDataFromDoctorGridView(List<object> list)
        {
 
            txtdoctorid.Text = list[0].ToString();
            txtdoctorname.Text = list[1].ToString();
 
        }
This question has already been answered. Start a new discussion instead.