从数据表填充文本框

时间:2012-02-23 18:41:27

标签: c# winforms

在我的Windows窗体中,我有两个组合框和一个文本框,当从cmbjobecode中选择一个jobecode时,它将加载带有相应引文的cmbquotationcode,并用选定的引用量填充文本框txtamount

一切都很好,除非我无法获得文本框中填写的金额,任何人都可以帮助排序错误

 private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboQuotationboxload();
        }

public void comboQuotationboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();

            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + "", oleDbConnection1);



            OleDbDataReader reader = oleDbCommand1.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Columns.Add("quotationpk", typeof(int));
            dt.Columns.Add("quotationcode", typeof(string));
            dt.Columns.Add("amount", typeof(int));
            dt.Load(reader);
            cmbQuotationcode.ValueMember = "quotationpk";
            cmbQuotationcode.DisplayMember = "quotationcode";
            cmbQuotationcode.DataSource = dt.DefaultView;
            txtamount.text= "amount";


            oleDbConnection1.Close();

        }

3 个答案:

答案 0 :(得分:1)

你试过上桌吗?但是?

        DataTable dt = new DataTable();
        DataRow row = table.Rows[0];
        dt.Columns.Add("quotationpk", typeof(int));
        dt.Columns.Add("quotationcode", typeof(string));
        dt.Columns.Add("amount", typeof(int));
 //then you could assign the textbox like this
 txtamount.text= (string)row["amount"]; 

这样的事情会引导你找到正确的答案 你总是期望只获得1个金额..?如果没有,那么你需要将该代码包装在循环中..

* 就个人而言,我会使用OleDbDataReader,它会读取列和字段,而不必像你一样添加字段..

这是一个如何使用OleDbDataReader的示例 我有一个我从DataBase写入GetNames的方法,例如

public void comboQuotationboxload()
{
     OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
     oleDbConnection1.Open();

     OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + "", oleDbConnection1);

     OleDbDataReader reader = oleDbCommand1.ExecuteReader();
     reader.Read();
     cmbQuotationcode.ValueMember = "quotationpk";
     cmbQuotationcode.DisplayMember = "quotationcode";
     cmbQuotationcode.DataSource = reader;
     txtamount.text = reader["amount"].ToString();
     oleDbConnection1.Close();
}

答案 1 :(得分:1)

在课程范围内创建dt

DataTable dt = new DataTable();

///在Form_Load()

中向表添加列
    dt.Columns.Add("quotationpk", typeof(int));
    dt.Columns.Add("quotationcode", typeof(string));
    dt.Columns.Add("amount", typeof(int));

//然后执行填充操作

 private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboQuotationboxload();
        }

public void comboQuotationboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();

            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + "", oleDbConnection1);



            OleDbDataReader reader = oleDbCommand1.ExecuteReader();

            dt.Load(reader);
            cmbQuotationcode.ValueMember = "quotationpk";
            cmbQuotationcode.DisplayMember = "quotationcode";
            cmbQuotationcode.DataSource = dt.DefaultView;



            oleDbConnection1.Close();

        }

//过滤数据并显示在文本框中

 private void cmbQuotationcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataView dvDataTable = new DataView(dt);
dvDataTable.RowFilter = "quotationpk ='" + cmbQuotationcode.SelectedValue "'";
if(dvDataTable.Count > 0)
{
 txtamount.Text= Convert.ToString(dvDataTable["amount"]);
}
else
{ 
txtamount.Text = "0";
}
        }

答案 2 :(得分:0)

您不必使用DataTable。

假设只返回了一行,你可以这样做:

public void comboQuotationboxload()
{
    OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
    oleDbConnection1.Open();
    OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + "", oleDbConnection1);
    OleDbDataReader reader = oleDbCommand1.ExecuteReader();

    if (!reader.Read())
        return;

    cmbQuotationcode.ValueMember = "quotationpk";
    cmbQuotationcode.DisplayMember = "quotationcode";
    cmbQuotationcode.DataSource = reader;
    txtamount.text = reader["amount"].ToString();

    oleDbConnection1.Close();
}