从数据表填充文本框 [英] fill text box from datatable
问题描述
在我的Windows窗体中,当从cmbjobecode中选择一个jobecode时,我有两个组合框和一个文本框,它将加载具有相应引号的cmbquotationcode并用选定的报价量填充文本框txtamount
一切都很好,除了我无法使文本框充满数量之外,任何人都可以帮助您排序错误
In my windows form i have two combobox and one text box when a jobecode is selected from cmbjobecode it will load cmbquotationcode with corresponding quotations and fills a textbox txtamount with amount of selected quotation
all is fine except i cannot get the textbox filled with the amount can anyone help in sorting mistake
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();
}
推荐答案
第一件事.您应该真正使用参数化查询!如果选择的cmrjobcode
值是什么呢? --drop表用户:)
您可以按照以下步骤进行操作:
First things first. You should REALLY use Parameterized Queries! What if the selected value ofcmrjobcode
was something like ; --drop table users :)
You can do this as follows:
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from quotationmastertable where jobpk = @jobpk", oleDbConnection1);
oleDbCommand1.Parameters.AddWithValue("@jobpk", cmbjobcode.SelectedValue);
用提供给 ^ ]. oledbcommand.parameters.aspx>命令的参数集合 [ ^ ].
现在,您的代码更易于阅读,查询可能会执行得更快(参数化查询会带来一些性能提升),并且您的代码是 SQL注入 [^ ]安全!
其次,如果implement
IDisposable ,则您应该真正将objects
中的dispose
> [< ^ ].连接和命令对象就是这种情况.您可以使用使用关键字 [oleDbCommand1.Dispose [.我确定您希望做的是放入当前选择的 DataRow [ ^ ]来自您的数据表 [TextBox.Text [ ^ ].
您可能应该做的是创建一个 BindingSource [ ^ ]并将您的数据表分配给 ^ ].现在,您可以将ComboBox
的DataSource
设置为指向BindingSource
,然后可以添加 ^ ]使用 ^ ](继承自控件 [^ ]).您可以在设计器中设置大多数属性.
在您的代码中只需添加:
This will replace any parameter in your query string
(in this case @jobpk
) with the value of the object
provided to the AddWithValue Method[^] of your Parameter collection of your Command[^].
Your code is now better to read, your query will possibly execute faster (parameterized queries have some performance gain) and your code is Sql Injection[^] safe!
Second, you should really dispose
of your objects
if they implement
IDisposable[^]. This is the case for you connection and command objects. You can do this using the using keyword[^] or by calling oleDbConnection1.Dispose
and oleDbCommand1.Dispose[^].
Also, you should better name your variables
and objects
. For example, cmbjobcode
is hard to read. cmbJobCode
reads much better!
Now to answer your question, you are currently simply setting the text to the literal string "amount"
. What I am sure you wish to do is put the amount of the currently selected DataRow [^]from your DataTable[^] in the TextBox.Text[^].
What you should probably do is create a BindingSource[^] and assign your DataTable to the DataSource Property[^]. You can now set the DataSource
of your ComboBox
to point at the BindingSource
and you can add a Binding[^] to your TextBox
using TextBox.DataBindings[^] (Inherited from Control[^]). You can set most of the properties in your designer.
In your code just add:
bindingSource.DataSource = dt;
我认为应该可以解决问题.祝你好运! :)
I think that should do the trick. Good luck! :)
最终我做到了
txtamount.text = dt.Rows [0] cells [2] .Tostring();
finally I did it
txtamount.text= dt.Rows[0]cells[2].Tostring();
这篇关于从数据表填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!