如果我设置属性:
EnableRowVirtualization="True"
RowDetailsVisibilityMode="VisibleWhenSelected"
SelectionMode="Extended"
SelectionUnit="Cell"
我的数据网格,然后:
int parse = dataGrid1.SelectedIndex;
返回“-1”,为什么?
答案 0 :(得分:5)
你没有在网格中选择任何东西 - 记住它是一个从零开始的索引,所以第一个 row / item将返回“0”。根据{{3}}:
如果选择为空,则SelectedIndex返回-1
您的下一行代码应为
if (parse >= 0)
{
//...
}
答案 1 :(得分:1)
如果未选择任何内容,此类大多数控件都会将SelectedIndex
作为-1
返回。 (例如,ListBox
和ComboBox
也是如此。)
答案 2 :(得分:0)
如果数据网格SelectionUnit =" Cell"然后你必须使用
int parse = datagrid1.Items.IndexOf(datagrid1.CurrentItem)
如果datagrid SelectionUnit =" FullRow"然后你可以使用
int parse = datagrid1.SelectedIndex