我正在使用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
答案 0 :(得分:1)
您可以将从数据库中检索的整个行直接存储在单个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集合。您应该始终从基础组合的数据源中添加/删除/删除元素
答案 1 :(得分:0)
通过添加其他两个组合框的添加行来完成此工作。
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;
}