Combox绑定数据

Posted 路上有你F

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Combox绑定数据相关的知识,希望对你有一定的参考价值。

  private void button3_Click(object sender, EventArgs e)
        {
            string sql = "select dname,did from department";

            SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=hem09;User ID=sa;Password=123456");
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader reader= cmd.ExecuteReader();

                //将数据绑定到列表框中
                cboList.Items.Clear();
                //通过循环读取每一行的数据
                while (reader.Read())//延迟执行
                {
                    //使用索引器读取数据
                    //reader[1]
                    //reader["dname"]
                    //使用方法的方式读取,不需要进行类型转换
                    string dname=reader.GetString(0);//参数为列的索引
                    //注意:这里的索引,不是根据表的结构来确定的,而是根据select子句的结果集决定的
                    cboList.Items.Add(dname);
                }
                reader.Close();
                reader.Dispose();
                cmd.Dispose();

                //cmd.CommandText = "";
            }
            catch
            {
                //...
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

  private void cbTitle_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = cbTitle.SelectedItem.ToString();//下拉框值传入其它组件中
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sql = "select dname,did from department";
            using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=hem09;user id=sa;Password=123456"))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();//尽量晚的打开连接,尽量早的关闭连接
                SqlDataReader reader = cmd.ExecuteReader();

                //将数据绑定到列表
                cboList.Items.Clear();
                while (reader.Read())
                {
                    Department d1 = new Department() {Did=reader.GetInt32(1),Dname=reader.GetString(0)};
                    cboList.Items.Add(d1);
                    cboList.DisplayMember = "Dname";//指定对象的某个属性用于显示
                }
                reader.Close();
                reader.Dispose();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //1、构造sql语句
            string sql = "select * from department";
            //2建立连接
            using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=hem09;User id=sa;Password=123456"))
            {
                //3.1构造命令对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //2.1打开连接
                conn.Open();
                //3.2执行命令
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    //清空原有数据
                    gvList.Rows.Clear();
                    gvList.Columns.Clear();
                    //新建列
                    gvList.Columns.Add("did","编号");
                    gvList.Columns.Add("dname", "名称");
                    //遍历查询结果集,将数据添加到列表中
                    while (reader.Read())
                    {
                        Department d1 = new Department() { Did = reader.GetInt32(0), Dname = reader.GetString(1) };
                        gvList.Rows.Add(d1.Did, d1.Dname);
                    }
                }
                cmd.Dispose();
            }
        }

          
       

以上是关于Combox绑定数据的主要内容,如果未能解决你的问题,请参考以下文章

C# 控制datagridview的combox属性的列绑定数据

WPF——ComboBox控件怎么绑定数据

c#中我想在combox里绑定数据库中某字段的内容,并显示在combox的下拉框中,请问大家该怎么去弄呢?

C# Winform Combox的绑定问题

C#中 combox控件已绑定数据, 如何根据一个string类型字段 设置combox的选择项

C# 中一个Panel有多个Combox要怎样从数据库绑定?