1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// ajouter :
// -un bouton selection(btnSelect)
// -le dgv source(dgvSrc)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinCloningDGV
{
public partial class Form2 : Form
{
//variable qui store DGV Destination transmis en parametre
private DataGridView dgv;
public Form2()
{
InitializeComponent();
}
public Form2(DataGridView pDGV):this()
{
dgv = pDGV;
btnSelect.Enabled = false;
// BindingSource est necessaire car ici j'utilise prop
// DataSource pour remplir le DGV Source
// Sans lui je ne peux pas supprimer des lignes du DGV Source
// AUTREMENT si tu remplis DGV source sans passer par un DataSource
// pas besoin du BindingSource
BindingSource bds=new BindingSource();
bds.DataSource = GetData(); //Obtient les donnees
this.dgvSrc.DataSource = bds;
}
// Chargement:copie des Columns
// DGV Source => dans DGV destination
private void Form2_Load(object sender, EventArgs e)
{
if (dgv == null) return;
btnSelect.Enabled = true;
DataGridViewColumnCollection cols = this.dgvSrc.Columns;
foreach (DataGridViewColumn col in cols)
{
dgv.Columns.Add((DataGridViewColumn)col.Clone());
}
this.dgv.Invalidate();
}
//index de colonne courante(voir plus loin)
private int oldRowIndex = -1;
private void btnSelect_Click(object sender, EventArgs e)
{
dgvSrc.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgvSrc.MultiSelect = true;
if (oldRowIndex == -1) return;
DataGridViewCellCollection cells =this.dgvSrc.Rows[oldRowIndex].Cells;
//ajout une row
dgv.Rows.Add();
int index = dgv.Rows.Count-2;
for (int j = 0; j < cells.Count; j++)
{
dgv.Rows[index].Cells[j].Value = cells[j].Value;
}
//maj affichage
dgv.Update();
//remove row ajoute
//(il est remove de la liste data egalement car bds s'en occupe)
dgvSrc.Rows.RemoveAt(oldRowIndex);
//maj affichage
dgvSrc.Update();
}
//recupere l'index courant
private void dgvSrc_CurrentCellChanged(object sender, EventArgs e)
{
oldRowIndex = this.dgvSrc.CurrentCellAddress.Y;
}
//charge les data
private List<Person> GetData()
{
Person p;
List<Person> l = new List<Person>();
for (int i = 0; i < 10; i++)
{
p = new Person("FName" + i.ToString(), "LName" + i.ToString());
l.Add(p);
}
return l;
}
}
} |
Partager