我正在开发一个我正在使用comboBox和ListView的应用程序。我想在启动应用程序时从数据库中显示Listview中的记录。并且combBox用于过滤以在comViewBox指定的ListView中显示那些记录。问题是,当我启动我的应用程序时,它不会在ListView中显示任何内容(comboBox和ListView都是从数据库填充的)。虽然填充了comboBox,但未填充ListView。当我在comboBox中选择一个项目时,它会在listView中显示这些数据。我为dataBase连接使用了一个单独的类。我的代码如下。
private void EmployeeAttendence_Load(object sender, EventArgs e)
{
try
{
table = dbOperation.select("employs.emp_id, employs.emp_name, employs.emp_fname, designation.name from employs inner join designation on designation.id = employs.designation");
listView1.Items.Clear();
foreach (DataRow row in table.Rows)
{
listView1.Items.Add(row[0].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[1].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[2].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[3].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add("P");
}
table = dbOperation.select("* from designation");
comboBox1.Items.Clear();
comboBox1.DataSource = table;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
这是comboBox过滤编码。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
table = dbOperation.select("employs.emp_id, employs.emp_name, employs.emp_fname, designation.name from employs inner join designation on designation.id = employs.designation where employs.designation = '" + comboBox1.SelectedValue + "'");
listView1.Items.Clear();
foreach (DataRow row in table.Rows)
{
listView1.Items.Add(row[0].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[1].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[2].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[3].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add("P");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
这是我的数据库选择查询编码。
public DataTable select(string table_name)
{
table = new DataTable();
command.CommandText = "select " + table_name;
command.Connection = con;
con.Open();
adapter.SelectCommand = command;
adapter.Fill(table);
con.Close();
return table;
}
答案 0 :(得分:1)
我认为您正在使用Windows窗体...
我认为问题在于,当您第一次加载winform时,您希望使用组合框的第一项填充列表框
因此,在form_load事件中,您应该在组合框中选择索引
private void EmployeeAttendence_Load(object sender, EventArgs e)
{
try
{
table = dbOperation.select("employs.emp_id, employs.emp_name, employs.emp_fname, designation.name from employs inner join designation on designation.id = employs.designation");
listView1.Items.Clear();
foreach (DataRow row in table.Rows)
{
listView1.Items.Add(row[0].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[1].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[2].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[3].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add("P");
}
table = dbOperation.select("* from designation");
comboBox1.Items.Clear();
comboBox1.DataSource = table;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
comboBox1.SelectedIndex = 0; //this should raise the event comboBox1_SelectedIndexChanged(object sender, EventArgs e) and the listbox will be populated
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
答案 1 :(得分:0)
if(!Page.IsPostBack)
private void EmployeeAttendence_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
try
{
table = dbOperation.select("employs.emp_id, employs.emp_name, employs.emp_fname, designation.name from employs inner join designation on designation.id = employs.designation");
listView1.Items.Clear();
foreach (DataRow row in table.Rows)
{
listView1.Items.Add(row[0].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[1].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[2].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[3].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add("P");
}
table = dbOperation.select("* from designation");
comboBox1.Items.Clear();
comboBox1.DataSource = table;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}