c# Datagridview перенести значение выбранной строки в Listview
нужно перенести выбранную строку datagridview в listview, после перенесенные данные необходимо добавить в базу данных. какие действия необходимо выполнит для этого ? datagridview заполнятся из базы данных mysql.
public partial class Country : Form
{
public struct city
{
public int id;
public string name;
public string country;
public city(int _id, string _name, string _country)
{
id = _id;
name = _name;
country = _country;
}
}
public Country()
{
InitializeComponent();
}
private void Country_Load(object sender, EventArgs e)
{
using (MySqlConnection con = Condatabase.GetDBConnection())
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT city.id, city.name, country.name AS country FROM city LEFT JOIN country ON city.countryID = country.id", con);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AllowUserToAddRows = false;
listView1.Columns.Add("id", 40, HorizontalAlignment.Center);
listView1.Columns.Add("name", 90, HorizontalAlignment.Center);
listView1.View = View.Details;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
DataGridViewSelectedCellCollection rows = (DataGridViewSelectedCellCollection)e.Data.GetData(typeof(DataGridViewSelectedCellCollection));
// DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
foreach (DataGridViewRow row in dataGridView1.Rows)
{
listView1.Items.Add(row.Cells[1].Value.ToString());
dataGridView1.Rows.Remove(row);
}
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.Move;
}
}
}