从datagridview添加行到datagridview 2

时间:2014-12-18 10:38:25

标签: c# datagridview

我有一个来自的两个datagridviews。我需要从数据库获取数据到datagridview1(使用Select * from database ...)然后我想使用Selected Rows将数据从datagriwview添加到datagridview2。

首先我想解决这个问题以获取Selected Row的ID,当我在datagridview2中选择它在datagridview2中显示的行时,但当我选择另一行时,它在datagridview中更新,它不会添加为new行。我尝试了几种方法,但没有解决这个问题,有没有人帮我解决这个问题?感谢

private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
    {


        int id = Convert.ToInt32

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

        try
        {

            MySqlConnection conn = new MySqlConnection(connection);
            MySqlCommand command = start.CreateCommand();
            command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id = '" + id.ToString() + "'";

            conn.Open();
            MySqlDataAdapter oxu = new MySqlDataAdapter(command);
            DataTable dt = new DataTable();

                oxu.Fill(dt);
                dataGridView2.DataSource = dt;


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

3 个答案:

答案 0 :(得分:1)

解释非常简单:每次双击datagridview1的单元格时,都会用新的数据表替换旧的数据表。如果你想追加结果,你可以这样做:

MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand command = start.CreateCommand();
command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id = '" + id.ToString() + "'";
conn.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;

答案 1 :(得分:0)

由于您拥有datagridview1中的所有信息,因此您应该将所选行的内容复制到datagridrow2的新行中。

DataGridView基于包含DataTables的DataSet。 DataTable包含行。 您不能将一行从一个表移到另一个表。 相反,您必须创建一个新行并插入DataGridView2的DataTable

答案 2 :(得分:0)

private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {

                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 id, muayine_adi, sabit_qiymet 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_adi"].Value = dt.Rows[i]["muayine_adi"].ToString();
                            dataGridView2.Rows[rVal].Cells["sabit_qiymet"].Value = dt.Rows[i]["sabit_qiymet"].ToString();
                        }

                    }
                    start.Close();
                }
        }