Csharp:Windowsform using CheckedListBox Datasource

简介: /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void List
 /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListboxCheckboxForm_Load(object sender, EventArgs e)
        {

            //设置CheckedListBox中第i项的Checked状态
            
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(Guid));
            dt.Columns.Add("name", typeof(string));
            dt.Rows.Add(Guid.NewGuid(), "geovindu");
            dt.Rows.Add(Guid.NewGuid(), "duf");
            dt.Rows.Add(Guid.NewGuid(), "涂聚文");
            dt.Rows.Add(Guid.NewGuid(), "tujwen");
            

            //checkedListBox1.Items.Add("");
            //checkedListBox1.Items.Insert(0, "");
            checkedListBox1.DataSource = dt;
            checkedListBox1.DisplayMember = "name";
            checkedListBox1.ValueMember = "id";

            checkedListBox1.SetItemCheckState(1, CheckState.Checked);
        }
        /// <summary>
        /// 獲取選擇的項
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {



            //1
            string checkedText = string.Empty;
            for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++)
            {
                this.checkedListBox1.SetSelected(i, true);
                checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + "[" +this.checkedListBox1.SelectedValue.ToString()+"]";
            }
            MessageBox.Show(checkedText);

            //2
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {

                //如果checkedListBox1的第i项被选中,

                //则显示checkedListBox1对应的值

                if (checkedListBox1.GetItemChecked(i))
                {
                   // MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]) + "[" + this.checkedListBox1.SelectedValue.ToString()+"]");
 
                }

            }

            //3
            string strCollected = string.Empty;

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {

                if (checkedListBox1.GetItemChecked(i))
                {

                    if (strCollected == string.Empty)
                    {

                        strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);

                    }

                    else
                    {

                        strCollected = strCollected + "/" + checkedListBox1.GetItemText(checkedListBox1.Items[i]);

                    }

                }

            }
            //MessageBox.Show(strCollected);
        }


        /// <summary>
        /// 設定是否全選
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void select_all_CheckedChanged(object sender, EventArgs e)
        {
            if (select_all.Checked)
            {
                for (int j = 0; j < checkedListBox1.Items.Count; j++)
                    checkedListBox1.SetItemChecked(j, true);

            }
            else
            {
                for (int j = 0; j < checkedListBox1.Items.Count; j++)
                    checkedListBox1.SetItemChecked(j, false);

            }
        }
        /// <summary>
        /// 獲取選擇的項
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string checkedText = string.Empty;
            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
            {
                if (this.checkedListBox1.GetItemChecked(i))
                {
                    this.checkedListBox1.SetSelected(i, true);
                    checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") +"["+this.checkedListBox1.SelectedValue.ToString()+"]" + this.checkedListBox1.GetItemText(checkedListBox1.Items[i]);
                }
            }
            MessageBox.Show(checkedText);
        }
        /// <summary>
        /// 設置選擇項
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {


            checkedListBox1.DataSource = null;            
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(Guid));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("check", typeof(bool));

            dt.Rows.Add(Guid.NewGuid(), "geovindu",false);
            dt.Rows.Add(Guid.NewGuid(), "duf",true);
            dt.Rows.Add(Guid.NewGuid(), "涂聚文",false);
            dt.Rows.Add(Guid.NewGuid(), "tujwen",true);

            checkedListBox1.DataSource = dt;
            checkedListBox1.DisplayMember = "name";
            checkedListBox1.ValueMember = "id";  
            //
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                checkedListBox1.SetItemChecked(i, (bool)dt.Rows[i]["check"]);
            }

        }

目录
相关文章
|
4月前
|
Java 数据库 Spring
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
56 0
|
4月前
|
Java 数据库连接 Spring
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could
这个错误通常出现在使用Spring Boot进行数据库连接时。错误信息表明Spring Boot未能配置一个DataSource,因为没有指定'url'属性,并且没有发现默认的数据库连接。
178 0
|
druid Java
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
787 0
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
springboot: Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource
springboot: Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource
springboot: Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource
|
数据库
Failed to configure a DataSource: url attribute is not specified and no embedded datasource could...
Failed to configure a DataSource: url attribute is not specified and no embedded datasource could...
Failed to configure a DataSource: url attribute is not specified and no embedded datasource could...
|
存储 SQL
CDS view注解解析 - @Environment.systemField
下面的CDS view使用到了@Environment.systemField这个注解,定义了两个参数#SYSTEM_LANGUAGE和#USER。
189 0
CDS view注解解析 - @Environment.systemField
SAP ui5 ABAP repository handler class的 get_webcontent方法
SAP ui5 ABAP repository handler class的 get_webcontent方法
SAP ui5 ABAP repository handler class的 get_webcontent方法
|
Java 关系型数据库 MySQL
ApiBoot DataSource Switch 使用文档
ApiBoot是一款基于SpringBoot1.x,2.x的接口服务集成基础框架, 内部提供了框架的封装集成、使用扩展、自动化完成配置,让接口开发者可以选着性完成开箱即用, 不再为搭建接口框架而犯愁,从而极大的提高开发效率。
|
数据库 容器
Entity Framework Core(3)-配置DbContext
设计时 DbContext 配置 EF Core 设计时工具如迁移需要能够发现和创建的工作实例DbContext以收集有关应用程序的实体类型以及它们如何映射到数据库架构的详细信息的类型。 此过程可以为自动,只要该工具可以轻松地创建DbContext,会将其配置同样到它如何将配置在运行时的方式。
945 0