将行从数据网格视图 1 添加到数据网格视图 2

本文关键字:数据网 视图 数据 网格 添加 | 更新日期: 2023-09-27 18:34:31

我有两个数据网格视图。我需要将数据从数据库获取到 datagridview1(使用 Select *from database...(,然后我想使用选定的行将数据从 datagriwview 添加到 datagridview2。

首先,我想解决这个问题以获取所选行的 ID,当我在数据网格视图中选择行时,它

显示在 datagridview2 中,但是当我选择另一行时,它在数据网格视图中更新,它不会添加为新行。我尝试了几种方法但没有解决这个问题,有人帮我解决这个问题吗?谢谢

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);
        }
    }

将行从数据网格视图 1 添加到数据网格视图 2

解释很简单:每次双击 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;

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

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

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();
                }
        }