c# add selected row from datagridview1 to datagridvierw2

  • Thread starter Thread starter Ali Nom
  • Start date Start date
A

Ali Nom

Guest
I have two datagridviews in one from. I need to get data from database to datagridview1 (using Select *from database...) then I want to add data from datagriwview1 to datagridview2 using Selected Rows.

First I wanted to solve this problem to get Selected Rows ID, and It works fine, but, when I select same row it adds twice. How can I check if Selected row already exists in datagridview2. Is there anyone to solve this problem and to give me an example? I am new (( Thanks

int id = Convert.ToInt32

(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value);//3

MySqlConnection start = new MySqlConnection(baglanti);
MySqlCommand command = start.CreateCommand();
command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler WHERE id = " + id.ToString() + "";
start.Open();
MySqlDataAdapter oxu = new MySqlDataAdapter(command);
DataTable dt = new DataTable();
oxu.Fill(dt);
if (dataGridView2.DataSource != null)
{
DataTable pr = dataGridView2.DataSource as DataTable;
pr.Merge(dt);
dataGridView2.DataSource = pr;
}
else
dataGridView2.DataSource = dt;

Continue reading...
 
Back
Top