Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 datagriwview to datagridview2 using Selected Rows.

First I wanted to solve this problem to get Selected Row's ID, when I select row in datagridview it shows in datagridview2, but when I select another row, it is updating in datagridview, it does not add as new row. I tried several ways but did not solve this problem, Is there anyone help me to solve this problem? Thanks

C#
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;
Posted
Comments
Check what is pr.Rows.Count inside the if clause?
Member 11284808 19-Dec-14 2:51am    
it does not work

If i understand you well...

The quickest way is to use Linq[^]:
C#
//get selected rows
var qry = from DataGridViewRow dgvr in DataGridView1.SelectedRows
    select dgvr;
DataGridView2.DataSource = qry;

Note: not tested! Just an idea.

Second solution is here: How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control[^]. You need to loop through the collection of selected rows and using StringBuilder class build comma separated list of ids. Finally, you need to pass this as a CommandText:
C#
//before loop
StringBuilder sb = new StringBuilder();
//inside loop
sb.Append(dataGridView1.SelectedCells[i].Value.ToString());
//outside the loop
command.CommandText = String.Concat("SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id IN (", sb.ToString(),")");
//further code


Note: before you use second solution, i need to warn you about SQL Injection[^].
 
Share this answer
 
Comments
Member 11284808 19-Dec-14 4:33am    
Dear, Maciej Los
Colud you give me an example, please? I am new, and I dont know how I have to use your solution?
Maciej Los 19-Dec-14 5:22am    
What kind of example?
First method is complete! Second method - i gave you a link and part of code which corresponds to your issue?
What else you want?
It is another way I used it, but It did not work neither. How can I check if value already exists in datagridview2? When I select row from datagridview1 it shows in datagridview2, but when I select same row twice, it repeats ((((

C#
if (dataGridView1.CurrentRow.Cells["Id"].Value != null)
                {
                    int Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Id"].Value);

                    MySqlConnection start = new MySqlConnection(baglanti);
                    MySqlCommand command = start.CreateCommand();
                    command.CommandText = "SELECT muayine_alt_qrup_id FROM tibbi_xidmetler  WHERE id = '" + Id + "'";
                    start.Open();
                    MySqlDataAdapter oxu = new MySqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    oxu.Fill(dt);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        int idx = dataGridView2.Rows.Count - 1;
                        dataGridView2.Rows.Add(dt.Rows.Count);
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            int rVal = (idx + i) + 1;
                          //  dataGridView2.Rows[rVal].Cells["id"].Value = dt.Rows[i]["id"].ToString();
                            dataGridView2.Rows[rVal].Cells["muayine_alt_qrup_id"].Value = dt.Rows[i]["muayine_alt_qrup_id"];
                           // dataGridView2.Rows[rVal].Cells["sabit_qiymet"].Value = dt.Rows[i]["sabit_qiymet"].ToString();
                        }

                    }
                    start.Close();
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900