如何从DataTable获取值到组合框?

时间:2016-02-27 03:54:38

标签: c# combobox datatable

我在C#中编写winform应用程序。使用带有DataTable的组合框时出现问题。 我有DataTable喜欢:

**privilege**       **username**       **password**

  admin                admin               8887abc

  user                 user1               abc123

我将其加载到combobox。只有展示权限列,当用户点击管理员或用户时,它会自动加载用户名密码,对应行用户点击Textbox用户和密码。当用户点击选择权限时,我无法获得该事件。

我的代码如下:

DataTable dt = new DataTable();

cmbCategory.DisplayMember = "privilege";
cmbCategory.ValueMember = "ID";
cmbCategory.DataSource = dt;

foreach(DataRow rows in dt.Rows)
{
    txtUser.Text = rows["username"].ToString();
    txtPassword.Text = rows["password"].ToString();
}

cmbCategory.SelectedIndex = 0;

我从excel文件加载DataTable。 这只是我的所有代码。 1个类在Excel中读取数据,将Excel数据从路径加载到DataTable。

2 个答案:

答案 0 :(得分:2)

您需要将处理程序绑定到cmbCategory.SelectedIndexChanged,如下所示:

cmbCategory.SelectedIndexChanged += cmbCategory_SelectedIndexChanged;

然后定义一个方法

void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
{
   // get the selection
   DataRowView drv = (DataRowView)cmbCategory.SelectedItem;

   // and display the info
   txtUser.Text = drv["username"].ToString();
   txtPassword.Text = drv["password"].ToString();
}

答案 1 :(得分:1)

组合框控件有一个名为" SelectedIndexChanged"它只需要处理该事件,因此当项目发生变化时,您可以查找DataTable中的特定项目或获取组合的选定项目(它也将是数据项目的实例)然后您搜索所需列的值。

<强> Form1.cs的

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private static readonly DataTable MyData = new DataTable();

        public Form1()
        {
            InitializeComponent();

            //Assign the event handler
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var column1 = new DataColumn("privilege", Type.GetType("System.String"));
            var column2 = new DataColumn("username", Type.GetType("System.String"));
            var column3 = new DataColumn("password", Type.GetType("System.String"));

            MyData.Columns.Add(column1);
            MyData.Columns.Add(column2);
            MyData.Columns.Add(column3);

            var row0 = MyData.NewRow();
            row0["privilege"] = "Select an Item";
            row0["username"] = "";
            row0["password"] = "";

            var row1 = MyData.NewRow();
            row1["privilege"] = "admin";
            row1["username"] = "admin";
            row1["password"] = "8887abc";

            var row2 = MyData.NewRow();
            row2["privilege"] = "user";
            row2["username"] = "user1";
            row2["password"] = "abc123";

            MyData.Rows.Add(row0);
            MyData.Rows.Add(row1);
            MyData.Rows.Add(row2);

            comboBox1.DisplayMember = "privilege";
            comboBox1.ValueMember = "username";
            comboBox1.DataSource = MyData;
            comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var cb = (sender as ComboBox);

                var selectedItem = (cb.SelectedItem as DataRowView);

                label1.Text = selectedItem["username"].ToString();
                label2.Text = selectedItem["password"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
        }
    }
}