运行以下代码,我发现SelectedIndexChanged
方法在加载方法之前运行...如何更正此问题?我尝试重置事件但不起作用
namespace adotestquestion
{
public partial class Bill : Form
{
public Bill()
{
InitializeComponent();
}
string constring = "data source=NISHANT-PC ; initial catalog=NIK_DATABASE ; user id=xxxxxx ; password=xxxxxx";
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter adapter;
DataSet ds;
DataTable dt;
int qty, tax, total, price;
private void Bill_Load(object sender, EventArgs e)
{
con = new SqlConnection(constring);
cmd = new SqlCommand("select billid from bill", con);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "billid";
comboBox1.ValueMember = "billid";
MessageBox.Show(id.ToString());
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.Text);
int id;
id = Convert.ToInt32(comboBox1.Text);
con = new SqlConnection(constring);
cmd = new SqlCommand("select * from bill where billid=@id", con);
// cmd.Parameters.Add("@id", Convert.ToInt32(comboBox1.Text));
cmd.Parameters.Add("@id", id);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr[1].ToString();
textBox2.Text = dr[2].ToString();
textBox3.Text = dr[4].ToString();
textBox4.Text = dr[3].ToString();
textBox5.Text = dr[5].ToString();
textBox6.Text = dr[6].ToString();
textBox7.Text = dr[7].ToString();
textBox8.Text = dr[8].ToString();
textBox9.Text = dr[9].ToString();
textBox10.Text = dr[10].ToString();
}
}
}
}
答案 0 :(得分:1)
使用InitializeComponent
方法取消订阅活动,并在Form_Load
的末尾汇总。
答案 1 :(得分:1)
您可以简单地检查事件中组合框中是否确实选择了任何内容,而不是尝试更改事件等。这将使代码保持简单并且能够正常工作。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
// your existing code goes here
}
}
答案 2 :(得分:1)
第三种方法是创建一个私有的类级变量,如isFormLoading
,最初将其设置为true
,然后在false
结束时将其设置为Bill_Load
事件。
您可以在comboBox1_SelectedIndexChanged
以及其他任何需要的位置检查变量的值,以确定代码块是否应该运行。
但实际上,任何其他提供的答案都可行。
答案 3 :(得分:1)
问题:无论何时将项目绑定到Combobox
,SelectedIndexChanged
事件都会被触发。
解决方案:在SelectedIndexChanged
事件中,您需要识别Load
事件或由于项目选择更改而触发的事件。
您可以声明boolean
变量,并在控件进入true
事件时将其设置为Load
。
selectedIndexChanged
事件的仅在boolean
变量为false
时执行代码。
注意:在Load
事件结束时,再次将boolean
变量更改为false
,以便SelectionChanged
事件在ComboBox
实际选择时触发 bool loadevent = false;
private void Bill_Load(object sender, EventArgs e)
{
loadevent = true;
con = new SqlConnection(constring);
cmd = new SqlCommand("select billid from bill", con);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "billid";
comboBox1.ValueMember = "billid";
MessageBox.Show(id.ToString());
loadevent = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!loadevent)
{
MessageBox.Show(comboBox1.Text);
int id;
id = Convert.ToInt32(comboBox1.Text);
con = new SqlConnection(constring);
cmd = new SqlCommand("select * from bill where billid=@id", con);
// cmd.Parameters.Add("@id", Convert.ToInt32(comboBox1.Text));
cmd.Parameters.Add("@id", id);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr[1].ToString();
textBox2.Text = dr[2].ToString();
textBox3.Text = dr[4].ToString();
textBox4.Text = dr[3].ToString();
textBox5.Text = dr[5].ToString();
textBox6.Text = dr[6].ToString();
textBox7.Text = dr[7].ToString();
textBox8.Text = dr[8].ToString();
textBox9.Text = dr[9].ToString();
textBox10.Text = dr[10].ToString();
}
}
}
事件1}}。
试试这个:
{{1}}