Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#(wpf)datagridview数据检索_C#_Wpf - Fatal编程技术网

C#(wpf)datagridview数据检索

C#(wpf)datagridview数据检索,c#,wpf,C#,Wpf,我正在用c#wpf编程,我有一个包含更多列的datagridview,我想检索特定列的数据,所以我首先获取所选索引的索引,然后尝试获取列值。我的代码如下 在debug中,我看到indexx得到的结果是正确的,但当我试图得到列值时,它告诉我索引超出了范围。 我想问我做错了什么 int indexx=dtgrid.SelectedIndex; 字符串id=((dtgrid.Columns[9].GetCellContent(dtgrid.Items[indexx])作为文本块) .文本 .ToStr

我正在用c#wpf编程,我有一个包含更多列的datagridview,我想检索特定列的数据,所以我首先获取所选索引的索引,然后尝试获取列值。我的代码如下

在debug中,我看到indexx得到的结果是正确的,但当我试图得到列值时,它告诉我索引超出了范围。 我想问我做错了什么

int indexx=dtgrid.SelectedIndex;
字符串id=((dtgrid.Columns[9].GetCellContent(dtgrid.Items[indexx])作为文本块)
.文本
.ToString());
试试看
Int indexx=datagridview.CurrentCell.RowIndex

不要尝试以这种方式从DataGrids中检索数据。而是使用视图模型将数据绑定到TextBox,当您在TextBox中写入时,TextBox将更改该数据。请注意,只有当你的文本框失去焦点时,数据才会更新,你可以在网格的所有文本框上使用一个按键事件和一个定时器来绕过这个限制

我强烈建议你看看这个图书馆:

下面是一个类似问题的片段:

<DataGrid SelectedItem={Binding SelectedItem}/>


在这里,您将在选定单元格切换后立即获得对该单元格的引用。这是链接:

您有多少列?