根据C#中的数据表填充表单值 [英] populate form values based on data table in C#

查看:172
本文介绍了根据C#中的数据表填充表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在返回一行的数据表,然后设置datatable的主键,然后查找该列。然后,我想将从公司列返回的值分配到 txtCompany

I am returning a data table of one row, then i set the primary key of the datatable and then looking for that column. I then want to assign the value which is returned from the company column into txtCompany.

这是我试过的:

private void comboBox1_SelectedItem(object sender, EventArgs e)
{
    MessageBox.Show("Populating form");
    m_dbConnection.Open();

    DataTable dt = new DataTable();
    SQLiteCommand command = m_dbConnection.CreateCommand();
    command.CommandText = "select * from rdpdirectory where company = @company order by company asc";
    //
    command.Parameters.Add(new SQLiteParameter("@company", comboBox1.SelectedText));
    SQLiteDataAdapter db = new SQLiteDataAdapter(command);
    db.Fill(dt);
    //return dt;

    DataColumn[] keyColumns = new DataColumn[1];
    keyColumns[0] = dt.Columns["company"];
    dt.PrimaryKey = keyColumns;

    DataRow row = dt.Rows.Find("company");
    if (row != null)
        txtCompany.Text = row["company"].ToString();
}   


推荐答案

代码:

private void comboBox1_SelectedItem(object sender, EventArgs e)
        {
            MessageBox.Show("Populating form");
            m_dbConnection.Open();

            DataTable dt = new DataTable();
            SQLiteCommand command = m_dbConnection.CreateCommand();
            command.CommandText = "select * from rdpdirectory where company = @company order by company asc";
            //
            command.Parameters.Add(new SQLiteParameter("@company", comboBox1.SelectedText));
            SQLiteDataAdapter db = new SQLiteDataAdapter(command);
            db.Fill(dt);
            //return dt;


            DataColumn[] keyColumns = new DataColumn[1];
            keyColumns[0] = dt.Columns["company"];
            dt.PrimaryKey = keyColumns;

            DataRow row = dt.Rows[0];
            txtCompany.Text = row["company"].ToString();
            txtServer.Text = row["server"].ToString();
            txtUserName.Text = row["username"].ToString();
            txtPassword.Text = row["password"].ToString();
        }

这篇关于根据C#中的数据表填充表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆