我创建了这个表单:
这是此表单的代码源:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
namespace GESTION_DE_STAGE
{
public partial class RechercheEtablissement : DevExpress.XtraEditors.XtraForm
{
private BindingManagerBase db;
SQL sql = new SQL();
public RechercheEtablissement()
{
InitializeComponent();
}
private void RechercheEtablissement_Load(object sender, EventArgs e)
{
sql.cn.Open();
SqlDataAdapter daEtablissement = new SqlDataAdapter("Select * from Etablissement", sql.cn);
daEtablissement.Fill(sql.ds, "Etablissement");
db = this.BindingContext[sql.ds.Tables["Etablissement"]];
id.DataBindings.Add("text", sql.ds.Tables["Etablissement"], "Id_Etablissement");
nom.DataBindings.Add("text", sql.ds.Tables["Etablissement"], "Nom_Etablissement");
ville.DataBindings.Add("text", sql.ds.Tables["Etablissement"], "Ville_Etablissement");
dgv.DataSource = sql.ds.Tables["Etablissement"];
for(int i=0; i<sql.ds.Tables["Etablissement"].Rows.Count; i++)
idE.Properties.Items.Add(sql.ds.Tables["Etablissement"].Rows[i][0]);
}
private void first_Click(object sender, EventArgs e)
{
db.Position = 0;
}
private void last_Click(object sender, EventArgs e)
{
db.Position = sql.ds.Tables["Etablissement"].Rows.Count - 1;
}
private void previous_Click(object sender, EventArgs e)
{
db.Position --;
}
private void next_Click(object sender, EventArgs e)
{
db.Position ++;
}
private void idE_SelectedIndexChanged(object sender, EventArgs e)
{
//The code to update the DataGridView where ID = the selected Item
}
private void nomE_EditValueChanged(object sender, EventArgs e)
{
//The code to update the DataGridView where Nom = the String that the user is typing
}
private void villeE_EditValueChanged(object sender, EventArgs e)
{
//The code to update the DataGridView where Ville = the String that the user is typing
}
}
}
我的问题是,我想添加一个代码,在DataGrid中显示数据,具体取决于comboBox中的选定项目或Nom文本字段或Ville文本字段中的文本。
答案 0 :(得分:0)
//将事件处理方法与theSelectedIndexChanged事件相关联。
this.ComboBox1.SelectedIndexChanged +=
new System.EventHandler(ComboBox1_SelectedIndexChanged);
private void ComboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
show data in the dataGrid
}
答案 1 :(得分:0)
是的,这就是我想要的......我已经使用过这段代码,但它不起作用:
private void idE_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = sql.ds.Tables["Etablissement"].Clone();
DataRow drow = sql.ds.Tables["Etablissement"].NewRow();
sql.s = "Select * from Etablissement where Id_Etablissement = @Id_Etablissement";
SqlCommand cmd = new SqlCommand(sql.s, sql.cn);
cmd.Parameters.AddWithValue("@Id_Etablissement", idE.SelectedItem.ToString());
sql.dr = cmd.ExecuteReader();
while (sql.dr.Read())
{
drow[0] = sql.dr[0];
drow[1] = sql.dr[1];
drow[2] = sql.dr[2];
}
dt.ImportRow(drow);
dgv.DataSource = dt;
sql.dr.Close();
}
while (sql.dr.Read())
{
drow[0] = sql.dr[0];
drow[1] = sql.dr[1];
drow[2] = sql.dr[2];
}
dt.ImportRow(drow);
dgv.DataSource = dt;
sql.dr.Close();
}