C# 从数据表填充文本框

C# 从数据表填充文本框,c#,winforms,C#,Winforms,在我的windows窗体中,我有两个组合框和一个文本框。当从cmbjobecode中选择一个作业代码时,它将加载带有相应报价的cmbquotationcode,并用所选报价的金额填充文本框txtamount 一切都很好,只是我无法在文本框中填入金额。有人能帮忙分类错误吗 private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e) { comboQuotationboxl

在我的windows窗体中,我有两个组合框和一个文本框。当从cmbjobecode中选择一个作业代码时,它将加载带有相应报价的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();

        }

你试过坐在桌边吗?排

        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的示例 例如,我有一个从数据库写入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();
}

您不必使用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();
}

在类范围内创建dt

DataTable dt=新的DataTable()

///在表单_Load()中将列添加到表中

//然后进行填充操作

 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";
}
        }

sreenath我在下面为您粘贴了一个代码示例,如果使用得当,OleDbDataReader可以产生一些很棒的结果。。如果你需要帮助,请随时联系
 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";
}
        }