[英]C# fill combo box with postgreSQL select using Npgsql then populate other textboxes
我正在使用Npgsql程序集。
我有用SQLquery的结果填充datagridview的代码,我也希望它同时填充组合框,然后一旦选择,其他两个文本框将填充相同行值的其他两个字段。
controlp包含3个字段:
com pri not
add 1 adds a file
del 2 deletes a file
ame 3 amends a file
仅作为信息,连接字符串存储在另一个类中。
填充datagridview的当前代码:
string pquery = "SELECT * FROM controlp";
NpgsqlConnection conn = new NpgsqlConnection(Utils.ConnectionString);
conn.Open();
NpgsqlCommand pcmd = new NpgsqlCommand(pquery, conn);
NpgsqlDataAdapter pda = new NpgsqlDataAdapter();
pda.SelectCommand = pcmd;
DataTable pdt = new DataTable();
pda.Fill(pdt);
pDGV.DataSource = pdt;
conn.Close();
我创建了两个组合框来保存选定的索引值,然后再将它们放入文本框,例如,如果我在comComboBox
选择一个值,例如add
,我希望它在notComboBox
中填充“添加文件”,并在priComboBox
1
因为它们在同一行。
我已经尝试过此代码,但是在comComboBox中进行选择时出现错误:
string pquery = "SELECT * FROM controlp";
NpgsqlConnection conn = new NpgsqlConnection(Utils.ConnectionString);
conn.Open();
NpgsqlCommand pcmd = new NpgsqlCommand(pquery, conn);
NpgsqlDataAdapter pda = new NpgsqlDataAdapter();
pda.SelectCommand = pcmd;
DataTable pdt = new DataTable();
pda.Fill(pdt);
pDGV.DataSource = pdt;
for (int i = 0; i < pdt.Rows.Count; i++)
{
comComboBox.Items.Add(pdt.Rows[i].ItemArray[0].ToString());
}
conn.Close();
private void comComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
priComboBox.SelectedIndex = notComboBox.SelectedIndex = comComboBox.SelectedIndex;
notTextBox.Text = notComboBox.Text;
priTextBox.Text = priComboBox.Text;
}
错误:
System.ArgumentOutOfRangeException:InvalidArgument =值“ 0”对“ SelectedIndex”无效。 参数名称:SelectedIndex
您可以将从数据库检索的整个行直接存储在单个ComboBox中。 在这种情况下,您应该为组合框提供有关要用于在其列表和文本中显示的属性的信息。
最好的方法是定义一个“对数据库表进行建模”的类,然后使用在遍历结果时创建的实例填充此表的列表。
public class ControlP
{
public string Com {get;set;}
public int Pri { get; set; }
public string Not {get;set;}
}
......
List<ControlP> results = new List<ControlP>();
string pquery = "SELECT * FROM controlp";
// Don't forget to enclose the connection in a using statement. It is a
// disposable object and should be correctly destroyed when you finish to use it
using(NpgsqlConnection conn = new NpgsqlConnection(Utils.ConnectionString))
using(NpgsqlCommand pcmd = new NpgsqlCommand(pquery, conn))
{
conn.Open();
using(NpgsqlDataReader reader = pcmd.ExecuteReader())
{
while(reader.Read())
{
ControlP p = new ControlP();
p.Com = reader["com"].ToString();
p.Pri = Convert.ToInt32(reader["pri"]);
p.Not = reader["not"].ToString();
results.Add(p);
}
pDGV.DataSource = results;
comComboBox.DataSource = results;
comComboBox.DisplayMember = "Com";
comComboBox.ValueMember = "Pri";
}
}
此时,您可以使用SelectedIndexChanged事件来检索所选对象并读取其属性,而无需另外两个组合。
private void comComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(comComboBox.SelectedItem != null)
{
ControlP p = comComboBox.SelectedItem as ControlP;
notTextBox.Text = p.Not;
priTextBox.Text = p.Pri;
}
}
请记住,设置数据源时,不能使用Items集合。 您应该始终从基础组合的数据源中添加/删除/删除元素
通过添加其他两个组合框的添加行来完成此工作。
string pquery = "SELECT * FROM controlp";
NpgsqlConnection conn = new NpgsqlConnection(Utils.ConnectionString);
conn.Open();
NpgsqlCommand pcmd = new NpgsqlCommand(pquery, conn);
NpgsqlDataAdapter pda = new NpgsqlDataAdapter();
pda.SelectCommand = pcmd;
DataTable pdt = new DataTable();
pda.Fill(pdt);
pDGV.DataSource = pdt;
for (int i = 0; i < pdt.Rows.Count; i++)
{
comComboBox.Items.Add(pdt.Rows[i].ItemArray[0].ToString());
priComboBox.Items.Add(pdt.Rows[i].ItemArray[1].ToString());
notComboBox.Items.Add(pdt.Rows[i].ItemArray[2].ToString());
}
conn.Close();
private void comComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
priComboBox.SelectedIndex = notComboBox.SelectedIndex = comComboBox.SelectedIndex;
notTextBox.Text = notComboBox.Text;
priTextBox.Text = priComboBox.Text;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.