0

我创建了这个表格:

在此处输入图像描述

这是这种形式的代码源:

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

我的问题是我想添加一个代码,根据组合框中的所选项目或 Nom 文本字段或 Ville 文本字段中的文本,在 DataGrid 中显示数据。

4

2 回答 2

0

// 将事件处理方法与 SelectedIndexChanged 事件相关联。

this.ComboBox1.SelectedIndexChanged += 
new System.EventHandler(ComboBox1_SelectedIndexChanged);


private void ComboBox1_SelectedIndexChanged(object sender, 
        System.EventArgs e)
{
     show data in the dataGrid
}
于 2012-07-31T07:52:15.810 回答
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();
        }
于 2012-07-31T20:55:44.673 回答