您的位置:首页 > 产品设计 > UI/UE

CombBox 的selectedvalue 的第一次绑定时的问题

2012-09-16 16:39 176 查看
http://social.msdn.microsoft.com/Forums/zh-CN/csharpgeneral/thread/2eae1861-76fa-4f46-9142-0c37e59fa471

Hi,

I am trying to do it: when a combo is loaded or user selects an item, I want to read the SelectedValue of the combo
in order to get the ID of a table to get data.

I have the next code in the SelectedIndexChanged event of the combo:

if (comboCursoActual.SelectedValue==null)

return;

if (comboCursoActual.SelectedValue.ToString() == "")

return;

string sql = "SELECT CENTRO_ID FROM AÑO_ACADEMICO WHERE ID=" + comboCursoActual.SelectedValue;

......

But when the combo is loaded (when form loaded) the SelectedValue contains the value of "System.Data.DataRowView"
insted of the ID (and sql statment crashes) . It hapens when the combo is populated with data from the database (databinding).

How can I fix it? Is possible that SelectedValue has not been yet filled with data?

Thanks

碰到了上述问题,下面有个方法可以:

DataRowView firstdrv = (DataRowView)productionSearchclasscb.ComboBox.SelectedValue;
string firstStr = firstdrv.Row.ItemArray[productionSearchclasscb.SelectedIndex].ToString();


搞定了。

以下是完整的程序片段:

 private void productionSearchclasscb_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = null;
DataRowView firstdrv = (DataRowView)productionSearchclasscb.ComboBox.SelectedValue; string firstStr = firstdrv.Row.ItemArray[productionSearchclasscb.SelectedIndex].ToString();
switch(productionSearchcb.SelectedIndex)
{
case -1://空白行
case 0://产品名称
case 1://产品拼音
case 2://产品条码
case 5://产品工厂编号
break;
case 3://产品分类

if (productionSearchclasscb.SelectedIndex > -1)
{
sql = "Select TOP 100 ProductionID,ProductionName,ProductionPingYin,ProductionPrice,ProductionBuyPrice,ProductionBarCode,ProductionClass,ProductionQty,ProductionFactory,ProductionMark,ProductionOrderQty,EnterDate From Pro_ProductionTable Where" +
" ProductionClass = "+firstStr;
try
{
commUse.DataGridViewReset(ProductionDGV);
ProductionDGV.DataSource = db.GetDataTable(sql, "ProductionTable");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "软件提示");
throw ex;
}
}
break;
case 4://产品工厂

if (productionSearchclasscb.SelectedIndex > -1)
{
sql = "Select TOP 100 ProductionID,ProductionName,ProductionPingYin,ProductionPrice,ProductionBuyPrice,ProductionBarCode,ProductionClass,ProductionQty,ProductionFactory,ProductionMark,ProductionOrderQty,EnterDate From Pro_ProductionTable Where" +
" ProductionFactory = " + firstStr;
try
{
commUse.DataGridViewReset(ProductionDGV);
ProductionDGV.DataSource = db.GetDataTable(sql, "ProductionTable");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "软件提示");
throw ex;
}
}
break;

default:
break;
}
}


问题又来了,这样子是解决了一个初始化的时候会出System.Data.DataRowView的问题,但是再次选择列表中的数据的时候无法继续下去了。没法做了~!

只能再改,再改了下一个新的思路,初始化的时候会读列出错,那就屏蔽它去。

string sql = null;

if (productionSearchclasscb.SelectedIndex > -1)
{

if (productionSearchclasscb.ComboBox.SelectedValue.ToString() == "System.Data.DataRowView")
{
MessageBox.Show("初始化", "软件提示");
}
else
{
sql = "Select TOP 100 ProductionID,ProductionName,ProductionPingYin,ProductionPrice,ProductionBuyPrice,ProductionBarCode,ProductionClass,ProductionQty,ProductionFactory,ProductionMark,ProductionOrderQty,EnterDate From Pro_ProductionTable Where" +
" ProductionClass  = " + productionSearchclasscb.ComboBox.SelectedValue.ToString();
try
{
commUse.DataGridViewReset(ProductionDGV);
ProductionDGV.DataSource = db.GetDataTable(sql, "ProductionTable");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "软件提示");
throw ex;
}
}
}


脑壳痛。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: