从comboBox或textBox中选择时创建datagridview

时间:2014-06-24 18:49:52

标签: c# sql

我需要能够从comboBox或textBox中选择一个员工并将其应用于我的datagridview,这样我才能看到所选的员工。下面是我的代码,目前可以从表中选择所有内容。有什么想法吗?

//Report groupbox - load groupbox
    private void groupBox7_Enter(object sender, EventArgs e)
    {
        //Load Username
        using (OleDbConnection con = new OleDbConnection(constring))
        {
            try
            {
                string query = "SELECT TellerNum FROM Employee ORDER BY TellerNum ASC";
                OleDbDataAdapter da = new OleDbDataAdapter(query, con);
                con.Open();
                DataSet ds = new DataSet();
                da.Fill(ds, "Name");
                comboBox20.DisplayMember = "TellerNum";
                comboBox20.DataSource = ds.Tables["Name"];
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        using (OleDbConnection con = new OleDbConnection(constring))
        {
            this.sESSIONTableAdapter.Fill(this.trainingDBDataSet5.SESSION);
            try
            {
                con.Open();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            //Build DataGridView
            try
            {
                sqlAdapter = new OleDbDataAdapter("SELECT SessionName, PrincipleName, SessionDate, TellerNum, Comments, SessionKey FROM [SESSION] WHERE TellerNum = @teller ORDER BY TellerNum;", con);
                sqlCommand = new OleDbCommandBuilder(sqlAdapter);

                sqlAdapter.InsertCommand = sqlCommand.GetInsertCommand();
                sqlAdapter.UpdateCommand = sqlCommand.GetUpdateCommand();
                sqlAdapter.DeleteCommand = sqlCommand.GetDeleteCommand();

                dataset = new DataSet();
                sqlAdapter.Fill(dataset, "[SESSION]");
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = dataset.Tables["[SESSION]"];

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
                    dataGridView1[5, i] = linkCell;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
                dataGridView1[5, i] = linkCell;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

你可以使用DataView来过滤这样的数据(我已经在数据表中创建了两列,并将表绑定到组合框,但是数据视图绑定在datagridView上。然后我可以使用组合中的Id值来过滤数据视图将过滤网格。):

 private void GridFilterForm_Load(object sender, EventArgs e)
    {
        this.InitializeGrid();
    }

    private DataView dataView;
    private void InitializeGrid()
    {
        DataTable dataTable = new DataTable();  
        dataView = new DataView(dataTable);

        dataTable.Columns.Add("Id");
        dataTable.Columns.Add("Name");
        dataTable.Rows.Add("1", "Name1");
        dataTable.Rows.Add("2", "Name2");
        dataTable.Rows.Add("3", "Name3");
        dataTable.Rows.Add("4", "Name4");

        //Bind to dataView
        this.dataGridView1.DataSource = dataView;

        //make sure about the datatable to show all
        this.comboBox1.DataSource = dataTable;
        this.comboBox1.DisplayMember = "Name";
        this.comboBox1.ValueMember = "Id";

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.comboBox1.SelectedValue !=null)
        {
            //Filter the DataView
            string value=   this.comboBox1.SelectedValue as string;
            if (!string.IsNullOrEmpty(value))
            {
                //filter on id
                dataView.RowFilter = "Id = " + value;
            }
        }
        else
        {
            dataView.RowFilter = null;
        }
    }