JoeMarvel
JoeMarvel

Reputation: 41

Create a datagridview when you select from a comboBox or textBox

I need to be able to select an employee from a comboBox or textBox and have it apply to my datagridview so I can see only the employee selected. Below is my code I have that currently works to select everything from the table. Any ideas?

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

Upvotes: 0

Views: 478

Answers (1)

Avneesh
Avneesh

Reputation: 654

You can use the DataView to filter the data like this ( I have created 2 columns in the datatable and binded the table to the combobox but dataview on datagridView. I can then use the Id value from the combo to filter the dataview which will filter the grid.):

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

Upvotes: 1

Related Questions