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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
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 WinFilterTables
{
public partial class Form1 : Form
{
DataSet dataset = null;
BindingSource bindingSource1 = null;
BindingSource bindingSource2 = null;
DataView view1 = null;
DataView view2 = null;
DataTableCollection tables = null;
public Form1()
{
InitializeComponent();
}
//Populate des 2 Tables exemples
private void button1_Click(object sender, EventArgs e)
{
dataset = GetDS();
tables = dataset.Tables;
// table Reponse
view1 = new DataView(tables[0]);
bindingSource1 = new BindingSource();
bindingSource1.DataSource = view1;
dataGridView1.DataSource = bindingSource1;
bindingSource1.PositionChanged += new EventHandler(bindingSource1_PositionChanged);
// table Question
view2 = new DataView(tables[1]);
bindingSource2 = new BindingSource();
bindingSource2.DataSource = view2;
dataGridView2.DataSource = bindingSource2;
bindingSource2.PositionChanged += new EventHandler(bindingSource2_PositionChanged);
//text binde sur table Question
textBox1.DataBindings.Add(
new Binding("Text",
bindingSource2,
"ID_Question",
true,
DataSourceUpdateMode.OnPropertyChanged));
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
}
void textBox1_TextChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
//2 labels qui "pistent" les positions des 2 bindingsources
private int pos1 = -1;
void bindingSource1_PositionChanged(object sender, EventArgs e)
{
pos1 = bindingSource1.Position;
label2.Text = pos1.ToString();
}
private int pos2 = -1;
void bindingSource2_PositionChanged(object sender, EventArgs e)
{
pos2 = bindingSource2.Position;
label2.Text = pos2.ToString();
//DataRowView from view2(question)
DataRowView drView2 = (DataRowView) view2[bindingSource2.Position];
//row
DataRow row = drView2.Row;
string stringFilter = row[2].ToString();
//filtre sur view1
view1.RowFilter = "ID_Question ='" + stringFilter + "'";
}
//2 tables d'exemple
private DataSet GetDS()
{
DataSet ds = new DataSet();
DataTable dt = null;
dt = SetupColumnRows(2);
ds.Tables.Add(dt);
dt = SetupColumnRows(3);
ds.Tables.Add(dt);
return ds;
}
private DataTable SetupColumnRows(int n)
{
DataTable dt = null;
DataColumn dc =null;
DataRow dr =null;
if (n==2)
{
dt=new DataTable("Question");
dc = new DataColumn("ID_Question",typeof(string));
dc.Unique=true ;
dc.Caption="Question";
dt.Columns.Add(dc);
dc = new DataColumn("LibelleQuestion",typeof(string));
dc.Caption="LibelleQuestion";
dt.Columns.Add(dc);
dr=dt.NewRow();
for (int i = 0; i < 10; i++)
{
dr[0] = "Question" + i.ToString();
dr[1]="Libelle"+i.ToString();
dt.Rows.Add(dr);
dr=dt.NewRow();
}
}
else if (n == 3)
{
dt = new DataTable("Reponse");
dc = new DataColumn("ID_Reponse",typeof(string));
dc.Unique=true ;
dc.Caption="Reponse";
dt.Columns.Add(dc);
dc = new DataColumn("LibelleReponse",typeof(string));
dc.Caption="LibelleReponse";
dt.Columns.Add(dc);
dc = new DataColumn("ID_Question", typeof(string));
dc.Caption="Question";
dt.Columns.Add(dc);
dr = dt.NewRow();
for (int i = 0; i < 10; i++)
{
dr[0] = "Reponse" +i.ToString();
dr[1]="Libelle"+i.ToString();
dr[2]="Question"+i.ToString();
dt.Rows.Add(dr);
dr=dt.NewRow();
}
}
return dt;
}
}
} |
Partager