依赖组合框填充的问题
本文关键字:问题 填充 组合 依赖 | 更新日期: 2023-09-27 18:00:23
我有一个windows窗体,用户可以从组合框中选择布料参数,如country state city方法我的参数是:
typeName - typeNo - pattern - variant
R - 125 - 254978 - 1
R - 125 - 254979 - 1
R - 125 - 254979 - 2
Ri- 54 - 54658 - 1
Ri- 54 - 54658 - 2... etc.
我填写formLoad上的typeName组合框在typeNoCombobox之后填充typeNameCombobox_SelectedIndexChanged()事件像这样的模式组合框填充typeNoCombobox_SelectedIndexChanged()事件。。。
但typeNameCombobox_SelectedIndexChanged()中存在问题错误信息显示:
无法绑定由多部分组成的标识符"System.Data.DataRowView"。
和使用这种荒谬的方法
if (typeNameCombobox .SelectedValue.ToString() != "System.Data.DataRowView")
{
fill -> typeNoCombobox
}
尽管这四个组合框是相关的,但下面的组合框不会填充第一个负载。
我使用了第二种荒谬的方法
typeName.selectedIndex = 1;
typeName.selectedIndex = 0;
typeName.selectedIndex = 1;
typeName.selectedIndex = 0;
(我必须至少做4次,因为有四个相关的组合框)
在所有组合框填充后,如果用户更改typeName,则没有问题。中的所有依赖值都在更改
有什么方法可以把这个代码写清楚吗?
响应后编辑:
tipTur=类型名称tip=类型否desen=模式varyant=变体
这是我的原始代码
ConnectionClassOleDb cc = new ConnectionClassOleDb();
private void SiparisDetay_Load(object sender, EventArgs e) //formload
{
sorgu = "Select distinct tipTurId, tipTur from View_Tip order by tipTur";
cc.cbDoldur(sorgu, cbTipTur, "tipTur", "tipTurId");
cbTipTur.SelectedIndex = 1;
cbTipTur.SelectedIndex = 0;
cbTipTur.SelectedIndex = 1;
cbTipTur.SelectedIndex = 0;
}
private void cbTipTur_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbTipTur.SelectedValue.ToString() != "System.Data.DataRowView")
{
string sorgu = "SELECT distinct tipNo FROM tblTip where " +
" tipTurId = '" + cbTipTur.SelectedValue.ToString() + "' ORDER BY tipNo";
cc.cbDoldur(sorgu, cbTip, "tipNo", "tipNo");
}
}
private void cbTip_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbTip.SelectedValue.ToString() != "System.Data.DataRowView")
{
string sorgu = "SELECT desen FROM tblTip WHERE " +
" tipTurId = '" + cbTipTur.SelectedValue.ToString() +
"' AND tipNo = '" + cbTip.SelectedValue.ToString() + "' order by desen";
cc.cbDoldur(sorgu, cbDesen, "desen", "desen");
}
}
private void cbDesen_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbDesen.SelectedValue.ToString() != "System.Data.DataRowView")
{
string sorgu = "SELECT varyant FROM tblTip WHERE " +
" desen = '" + cbDesen.SelectedValue.ToString() +
"' AND tipTurId = '" + cbTipTur.SelectedValue.ToString() +
"' AND tipNo = '" + cbTip.SelectedValue.ToString() + "' ORDER BY varyant";
cc.cbDoldur(sorgu, cbVaryant, "varyant", "varyant");
}
}
连接类中的代码如下:
public void cbDoldur(string sorgu, ComboBox cb, string text, string value) //fill combobox
{
DataTable dtDdl = Dt(sorgu);
cb.DataSource = dtDdl;
cb.DisplayMember = text;
cb.ValueMember = value;
if (cb.Items.Count > 0)
{
cb.SelectedIndex = 0;
}
}
public DataTable Dt(string query)
{
DataTable dt = new DataTable();
if (Open())
{
OleDbDataAdapter da = new OleDbDataAdapter(query, connection);
try
{
da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show("Sistem Mesajı:" + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//connection.Close();
return dt;
}
您应该尝试将Alias添加到查询中的字段名中
Select distinct tipTurId, tipTur from View_Tip
至
Select distinct tipTurId as tipTurId , tipTur from View_Tip
在这种情况下,SELECT distinct tipNo FROM tblTip
到
SELECT distinct tipNo as tipNo FROM tblTip
因为它将无法在DataSource中找到作为tipTurId
的字段,因为它将使用Distinct返回,tipNo
也是如此
我解决了在类似的类中更改组合导出函数的问题
cb.DisplayMember = text;
cb.ValueMember = value;
cb.DataSource = dtDdl;