Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从组合框或文本框中选择时创建datagridview_C#_Sql - Fatal编程技术网

C# 从组合框或文本框中选择时创建datagridview

C# 从组合框或文本框中选择时创建datagridview,c#,sql,C#,Sql,我需要能够从组合框或文本框中选择一名员工,并将其应用于我的datagridview,以便我只能看到所选的员工。下面是我的代码,目前可以从表中选择所有内容。有什么想法吗 //Report groupbox - load groupbox private void groupBox7_Enter(object sender, EventArgs e) { //Load Username using (OleDbConnection con = new

我需要能够从组合框或文本框中选择一名员工,并将其应用于我的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;
            }
        }
    }
//报告groupbox-加载groupbox
私有void groupBox7_Enter(对象发送方,事件参数e)
{
//加载用户名
使用(OLEDB连接con=新OLEDB连接(构造))
{
尝试
{
string query=“通过Tellenum ASC从员工订单中选择Tellenum”;
OleDbDataAdapter da=新的OleDbDataAdapter(查询,con);
con.Open();
数据集ds=新数据集();
da.填写(ds,“名称”);
comboBox20.DisplayMember=“tellenum”;
comboBox20.DataSource=ds.Tables[“Name”];
con.Close();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
使用(OLEDB连接con=新OLEDB连接(构造))
{
this.sESSIONTableAdapter.Fill(this.trainingDBDataSet5.SESSION);
尝试
{
con.Open();
}
catch(Exception ex){MessageBox.Show(ex.Message);}
//构建DataGridView
尝试
{
sqlAdapter=新的OleDbDataAdapter(“从[SESSION]中选择SessionName、PrincipleName、SessionDate、TellerNum、Comments、SessionKey,其中TellerNum=@teller ORDER BY TellerNum;”,con);
sqlCommand=新的OleDbCommandBuilder(sqlAdapter);
sqlAdapter.InsertCommand=sqlCommand.GetInsertCommand();
sqlAdapter.UpdateCommand=sqlCommand.GetUpdateCommand();
sqlAdapter.DeleteCommand=sqlCommand.GetDeleteCommand();
数据集=新数据集();
sqlAdapter.Fill(数据集“[SESSION]”);
dataGridView1.DataSource=null;
dataGridView1.DataSource=dataset.Tables[“[会话]”];
对于(int i=0;i
您可以使用DataView像这样过滤数据(我在datatable中创建了两列,并将表绑定到组合框,但datagridView上没有DataView。然后,我可以使用组合框中的Id值过滤将过滤网格的DataView):


就像在搜索和筛选或搜索和选择?我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。如果我使用组合框,我会让它从列表中选择一名员工(我已经这样做了)。然后点击submit,它会创建一个关于该员工信息的datagridview。我可以使用文本框,但无论哪种方式都可能是相同的过程。我只是不知道如何让comboBox或textBox与datagridview相关联。我似乎在使用这段代码时遇到了问题。我无法让它生产任何东西。我添加了更多的代码,以便您可以看到我当前如何设置comboBox和datagridview。在网格上设置会话表数据源的地方,您需要使用dataview。Dataview在构造函数中获取datatable。当组合更改时,只需设置dataview rowfilter属性
 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;
        }
    }