使用 Npgsql 使用 postgreSQL 选择的 C# 填充组合框,然后填充其他文本框



我正在使用Npgsql程序集。

我有代码用SQL查询的结果填充数据网格视图,我还希望它同时填充一个组合框,然后在进行选择后,其他两个文本框填充相同行值的其他两个字段。

控件包含 3 个字段:

com pri not
add 1   adds a file
del 2   deletes a file
ame 3   amends a file

仅供参考,连接字符串存储在另一个类中。

填充数据网格视图的当前代码:

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"添加文件"并priComboBox1因为它们在同一行中。

我已经尝试过这段代码,但是当我在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=Value of '0' 对 '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;
}

最新更新