【问题标题】:How to get the first element of multicolumn combo box into grid view?如何在网格视图中获取多列组合框的第一个元素?
【发布时间】:2015-12-29 14:57:48
【问题描述】:

我正在使用 telerik 网格视图多列组合框

我想获取组合框的第一个元素并将其插入到网格视图的第一个单元格中,然后将其插入到数据库中

当我选择组合框元素时出现运行时错误

对象引用未设置为对象的实例

这是它出现的地方(在最后一行)

    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

这是导致此运行时错误的代码

private void radMultiColumnComboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string text;
        text = radMultiColumnComboBox3.SelectedValue.ToString();
        radGridView1.Rows.Add(Text);
    }

【问题讨论】:

标签: c# winforms combobox telerik runtime-error


【解决方案1】:

首先,组合框的第一个元素不一定在SelectedValue中。

text = radMultiColumnComboBox3.SelectedValue.ToString(); //this doesn't get the first value

顾名思义,SelectedValue 是组合框的选定值,它不是它的第一个元素。

其次,您的ComboBox 可能具有选定的值并具有SelectedValue == null。因此SelectedValue.ToString() 无法执行。

要查看ComboBox 是否有项目,请使用Items.Count

if (radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows.Count > 0){
    //do something, item exists
    //check also SelectedIndex of the combo box, if it is == -1, nothing is selected
} else {
    //This combobox does not have any item
}

要获取第一个元素,你可以这样做,

var item = radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows[0]; //item is the first element, provided the count > 0

【讨论】:

  • visual studio 不知道(项目)
  • 也许您可以先将其作为对象,然后将其转换为其他对象。我编辑了我的答案
  • 我确实看过你的回答,铸造是什么意思?
  • 类型转换:msdn.microsoft.com/en-us/library/ms173105.aspx。从一种类类型更改为另一种类类型,例如 int b = 5; double a = (double)b;
  • 它仍然不知道该项目
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 2022-11-27
  • 2021-12-08
  • 2019-04-19
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多