zoukankan      html  css  js  c++  java
  • datatable绑定comboBox显示数据[C#]

     

    实现功能:

         datatable绑定comboBox,在下拉菜单中显示对应数据

    实现方法:

         1、生成datatable,并为combox绑定数据源:

                comboBox1.DataSource = dt1;
                comboBox1.DisplayMember = "用户编码";
                comboBox1.ValueMember = "ID";
                this.comboBox1.SelectedIndex = -1;

        2、在combox的SelectedIndexChanged事件中添加如下方法:

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int iCurrentIndex = this.comboBox1.SelectedIndex;
                if (iCurrentIndex < 0) return;

                DataRow dr = dt1.Rows[iCurrentIndex];
                iID = Int32.Parse(dr["ID"].ToString());

            }

    另:如果textBox也想绑定该数据源,实现连动(如输入编码显示对应名称)

        3、在textBox的TextChanged事件中添加如下方法:

            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                string strUserNo = this.textBox1.Text.Trim();
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (dt1.Rows[i]["用户编码"].ToString() == strUserNo)
                    {
                        this.comboBox1.SelectedIndex = i;
                        break;
                    }
                    else
                    {
                        this.comboBox1.SelectedIndex = -1;
                    }
                }
            }

  • 相关阅读:
    20205025模拟
    CDQ分治详解
    点分治详解
    虚树详解
    整体二分详解
    算法学习————线段树合并
    Mvc.ExceptionHandling.AbpExceptionFilter
    小程序前端转换时间格式2021-02-25T12:01:20.254748
    《生命3.0—在亿年的尺度下审视生命的演进》阅读笔记1
    软件杯赛题周总结(1)
  • 原文地址:https://www.cnblogs.com/louby/p/5103818.html
Copyright © 2011-2022 走看看