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
碰到了上述问题,下面有个方法可以:
搞定了。
以下是完整的程序片段:
问题又来了,这样子是解决了一个初始化的时候会出System.Data.DataRowView的问题,但是再次选择列表中的数据的时候无法继续下去了。没法做了~!
只能再改,再改了下一个新的思路,初始化的时候会读列出错,那就屏蔽它去。
脑壳痛。
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; } } }
脑壳痛。
相关文章推荐
- 关于mvvm的ComboBox绑定SelectedValue值不正确问题
- Android studio value 2 (com.android.dex.DexException:资源重复引用或者加载问题)
- DropDownList数据绑定及SelectedValue DataTextField 用法
- v-model绑定后设置selected问题
- DropDownList等列表类控件的SelectedValue设置出错的问题
- Mvvm combobox绑定Dictionary<Key,Value>问题
- DropDownList等列表类控件的SelectedValue设置出错的问题
- 因DataValueField设置不当引起的DropDownList的SelectedItem的取不到所选值的问题
- jquery使用live绑定toggle事件,第一次点击没有反应,第2次才起作用问题
- WPF Combox selectedItem”不能绑定”的问题
- angular4中ngModel双向绑定在限制输入情况下与value值不一致的问题及尝试的解决方法
- 枚举与Combox的SelectedIndex绑定出现怪异问题的解决方案
- 【UWP】FlipView绑定ItemsSource,Selectedindex的问题
- mybatis遇到的一个问题Result Maps collection already contains value for com.schooldevice.dao.DeviceMapper.
- 基于ASP.NET MVC 下的Extjs的Combbox加载速率问题,终于解决啦:)
- combobox2根据combobox1的值绑定,第一次加载时的问题
- 自定义的多级联动dropdownlist的selectedvalue不变化的问题
- github.com/tealeg/xlsx 问题"multiple-value cell.String() in single-value context" 解决
- v-model绑定后设置selected问题
- 因DataValueField设置不当引起的DropDownList的SelectedItem的取不到所选值的问题