簡體   English   中英

什么是允許用戶根據鍵入的內容在DataGrid中顯示數據的代碼?

[英]What is the code that allows the user to display data in a DataGrid depending on what is typed?

我創建了此表單:

在此處輸入圖片說明

這是這種形式的代碼源:

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

我的問題是,我想添加一個代碼,以根據comboBox或Nom文本字段或Ville文本字段中的所選項目在DataGrid中顯示數據。

//將事件處理方法與SelectedIndexChanged事件相關聯。

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


private void ComboBox1_SelectedIndexChanged(object sender, 
        System.EventArgs e)
{
     show data in the dataGrid
}

是的,這就是我想要的...我已經使用了此代碼,但無法正常工作:

 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();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM