gpt4 book ai didi

C# 组合框 SelectedValue Null

转载 作者:行者123 更新时间:2023-11-30 17:53:30 30 4
gpt4 key购买 nike

没有发布我的全部代码(我只是发布了有问题的脚本),我遇到了一个问题,似乎很多人都遇到过,但我的似乎没有响应其他推荐的修复程序。我在 VS2008 中使用 C#。

基本上我有一个组合框,当更改项目初始化时,它转到下面的代码。本质上,代码将确定选择了哪个国家/地区 (myCountryKey),然后将其作为参数传递给填充后续组合框的存储过程。

奇怪的是,cboCountries 的 selectedValue 属性总是显示为 Null。在阅读这个问题时,似乎 dropdownStyle 属性是问题所在,但我按照建议将我的更改为 DropDownList 并且没有用。

因为我经常使用下拉菜单,所以我开始玩 arond,发现我可以让 SelectedIndex 属性起作用,我也可以使用 GetItemText 属性来起作用(这就是 myCountryKey2 和 myCountryKey3 变量的用途)。然而,我真正想要的是 SelectedValue,我以前做过这样的事情,只是不明白为什么它不起作用。

是否有任何其他我可能不小心更改的组合框属性可能导致 SelectedValue 不起作用?

private void cboCountries_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = null;
SqlDataReader dr = null;

try
{
cmd = util.SqlConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;

myCountryKey = int.Parse(this.cboCountries.SelectedValue.ToString()); //does not work, says value is null
myCountryKey2 = int.Parse(this.cboCountries.SelectedIndex.ToString()); //Works fine
string myCountryKey3 = this.cboCountries.GetItemText(this.cboCountries.SelectedItem).ToString(); //Works fine

cboDivisions.Enabled = true;
cboDivisions.Items.Clear();

// Division parameter
cmd.CommandText = "proc_parms_division";
dr = cmd.ExecuteReader();
while (dr.Read())
this.cboDivisions.Items.Add(new ListItem(dr["division_name"].ToString(), dr["division_key"].ToString()));

dr.Close();
cmd.Parameters.Clear();
}
catch (Exception ex)
{
util.LogError(ex);
MessageBox.Show(ex.Message);
}
finally
{
if (dr != null) dr.Dispose();
if (cmd != null) cmd.Dispose();
}
}

更多代码:

       public frmWriteoff()
{
InitializeComponent();
//this.KeyPreview = true;
//this.KeyUp += new KeyEventHandler(frmWriteoff_KeyUp);

// Data objects are unmanaged code.
// Declare them above the try{} block and always dispose of them in finally{}.
SqlCommand cmd = null;
SqlDataReader dr = null;

try
{
this.cboCountries.DisplayMember = "Label";
this.cboCountries.ValueMember = "Value";
this.cboDivisions.DisplayMember = "Label";
this.cboDivisions.ValueMember = "Value";
this.cboBusinessUnits.DisplayMember = "Label";
this.cboBusinessUnits.ValueMember = "Value";
this.cboMCCs.DisplayMember = "Label";
this.cboMCCs.ValueMember = "Value";
this.cboNodes.DisplayMember = "Label";
this.cboNodes.ValueMember = "Value";
this.cboProductCodes.DisplayMember = "Label";
this.cboProductCodes.ValueMember = "Value";


// Country parameters
cmd = util.SqlConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "proc_parms_countries_for_user";
cmd.Parameters.Add("@whoAmI", SqlDbType.VarChar).Value = WindowsIdentity.GetCurrent().Name;
dr = cmd.ExecuteReader();
while (dr.Read())
if (dr["country key"].ToString() != "0" && dr["country key"].ToString() != "1")
{
this.cboCountries.Items.Add(new ListItem(dr["country name"].ToString(), dr["country key"].ToString()));
}
cmd.Parameters.Clear();
dr.Close();

}
catch (Exception ex)
{
util.LogError(ex);
MessageBox.Show(ex.Message);
}
finally
{
if (dr != null) dr.Dispose();
if (cmd != null) cmd.Dispose();
}
}

最佳答案

我想通了,未绑定(bind)的数据又来了。当我更改我的代码以将我的国家/地区列表加载到数据表,然后将该数据表用作我的组合框的来源时,一切都很好。

    public frmWriteoff()
{
InitializeComponent();
//this.KeyPreview = true;
//this.KeyUp += new KeyEventHandler(frmWriteoff_KeyUp);

// Data objects are unmanaged code.
// Declare them above the try{} block and always dispose of them in finally{}.
SqlCommand cmd = null;
SqlDataReader dr = null;

try
{
this.cboCountries.DisplayMember = "Label";
this.cboCountries.ValueMember = "Value";
this.cboDivisions.DisplayMember = "Label";
this.cboDivisions.ValueMember = "Value";
this.cboBusinessUnits.DisplayMember = "Label";
this.cboBusinessUnits.ValueMember = "Value";
this.cboMCCs.DisplayMember = "Label";
this.cboMCCs.ValueMember = "Value";
this.cboNodes.DisplayMember = "Label";
this.cboNodes.ValueMember = "Value";
this.cboProductCodes.DisplayMember = "Label";
this.cboProductCodes.ValueMember = "Value";


// Country parameters
cmd = util.SqlConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "proc_parms_countries_for_user";
cmd.Parameters.Add("@whoAmI", SqlDbType.VarChar).Value = WindowsIdentity.GetCurrent().Name;
dr = cmd.ExecuteReader();

/////////////////////////////////////////////////////////////////////////////////////////////////

var dtCountries = new DataTable();
dtCountries.Columns.Add("Label");
dtCountries.Columns.Add("Value");

//DataRow _countries = dtCountries.NewRow();
//_countries["country key"] = myBusinessUnit;
//_countries["country name"] = myDataYear;

//dtCountries.Rows.Add(_fcst);

while (dr.Read())
if (dr["country key"].ToString() != "0" && dr["country key"].ToString() != "1")
{
dtCountries.Rows.Add(dr["country name"].ToString(), dr["country key"].ToString());
}
cmd.Parameters.Clear();
dr.Close();

this.cboCountries.DataSource = dtCountries;

关于C# 组合框 SelectedValue Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17302982/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com