如何在WPF中从datagrid保存数据列 [英] How do I save a column of a data from datagrid in WPF
问题描述
我想将数据网格中的一列数据保存到列表中
例如这些数据已经输入到数据网格中
ID |名称|号码
-----------------------
1 |大卫| 3012525
2 |安娜| 5825926
3 |安娜| 5878926
我想将数字保存在列表中,以便我可以在其他地方使用它们吗?
喜欢
数字
3012525
5825926
5878926
谢谢
我尝试过:
我搜索过并且显然在winform中你可以这样做:
List< double> Number = new List< double>();
foreach ( var 行 dataGridView1.row)
{
Number.Add(Convert.ToDouble(row.Cells [ 2 ]));
} < / double > < / double >
Yo可以使用以下方法读取单元格值(需要做进一步修改)
var NumberList = new List< double>();
< pre lang = C#> foreach( var row in dataGridView1.row)
{
/// 使用适当的参数
< pre lang = 调用以下方法 C#> NumberList.Add(Convert.ToDouble( value 返回 来自方法));
}
& amp; lt; pre lang =& amp; quot; C#& amp; quot;& amp; gt; public string GetCellValue(DataGrid datagrid, int 行, int 列)
{
var cellInfo = new DataGridCellInfo(
datagrid.Items [row],dataGrid.Columns [column]);
DataGridCell cell = null ;
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent!= null )
cell =(DataGridCell)cellContent。父母;
if (cell == null )返回 string .Empty;
// 如果使用DataGridTextColumn / DataGridComboBoxColumn
// 或AutoGeneratedColumns为True
if (cell.Content TextBlock)
return ((TextBlock)cell.Content)。文本;
else if (cell.Content ComboBox)
return ((ComboBox)cell.Content).Text;
// 如果使用DataGridTemplateColumn
// 假设单元格是TextBox,TextBlock或ComboBox。其他类型可以采用相同的方式处理。
else
{
var txtPresenter = FindVisualChild& amp; lt; TextBox& amp; amp; gt;((ContentPresenter)cell.Content);
if (txtPresenter!= null ) return txtPresenter.Text;
var txbPresenter = FindVisualChild& amp; amp; lt; TextBlock& amp; amp; gt;((ContentPresenter)cell.Content);
if (txbPresenter!= null ) return txbPresenter.Text;
var cmbPresenter = FindVisualChild& amp; amp; lt; ComboBox& amp; amp; gt;((ContentPresenter)cell.Content);
if (cmbPresenter!= null ) return cmbPresenter.Text;
}
return string .Empty;
}
public static T FindVisualChild& amp; amp; lt; T& amp; amp; gt;(DependencyObject obj)其中 T:DependencyObject
{
for ( int i = 0 ; i& amp; amp; lt; VisualTreeHelper .GetChildrenCount(obj); i ++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj,i);
if (child!= null & amp; amp; amp;& amp; amp; amp; child T)
return (T)child;
else
{
T childOfChild = FindVisualChild& amp; amp; lt; T& amp; amp; gt;(child) ;
if (childOfChild!= null )
返回 childOfChild;
}
}
返回 null ;
}& amp; lt; / pre& amp; gt;& lt; / pre& gt; < / pre >
。
< blockquote>看看 ObservableCollection
,你可以用它来绑定。
改变工作方式,并且很容易获得值,在示例中 MyList
:
代码:
ObservableCollection< MyClass> MyList = new ObservableCollection< MyClass>();
表格:
< ListBox ItemsSource ={Binding MyList}>< / ListBox>
更多信息可在此处找到:如何:创建并绑定到ObservableCollection [ ^ ]
I want to save a column of data from a datagrid into a list
for example these data are already entered to a datagrid
ID | Name | Number
-----------------------
1 | David | 3012525
2 | Anna | 5825926
3 | Anna | 5878926
I want to save the numbers in a list so i can use them somewhere else?
Like
Number
3012525
5825926
5878926
Thanks
What I have tried:
I searched and apparently in winform you can do this :
List<double> Number= new List<double>();
foreach (var row in dataGridView1.row)
{
Number.Add(Convert.ToDouble(row.Cells[2]));
}</double></double>
Yo can read the cell value using below method and (need to do further modification)
var NumberList= new List<double>(); <pre lang="C#">foreach (var row in dataGridView1.row) { /// call the below method here with appropriate parameter <pre lang="C#">NumberList.Add(Convert.ToDouble(value return from method)); } &lt;pre lang=&quot;C#&quot;&gt;public string GetCellValue(DataGrid datagrid, int row, int column) { var cellInfo = new DataGridCellInfo( datagrid.Items[row], dataGrid.Columns[column]); DataGridCell cell = null; var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item); if (cellContent != null) cell = (DataGridCell)cellContent.Parent; if (cell == null) return string.Empty; // if DataGridTextColumn / DataGridComboBoxColumn is used // or AutoGeneratedColumns is True if (cell.Content is TextBlock) return ((TextBlock)cell.Content).Text; else if (cell.Content is ComboBox) return ((ComboBox)cell.Content).Text; // if DataGridTemplateColumn is used // assuming cells are either TextBox, TextBlock or ComboBox. Other Types could be handled the same way. else { var txtPresenter = FindVisualChild&amp;lt;TextBox&amp;gt;((ContentPresenter)cell.Content); if (txtPresenter != null) return txtPresenter.Text; var txbPresenter = FindVisualChild&amp;lt;TextBlock&amp;gt;((ContentPresenter)cell.Content); if (txbPresenter != null) return txbPresenter.Text; var cmbPresenter = FindVisualChild&amp;lt;ComboBox&amp;gt;((ContentPresenter)cell.Content); if (cmbPresenter != null) return cmbPresenter.Text; } return string.Empty; } public static T FindVisualChild&amp;lt;T&amp;gt;(DependencyObject obj) where T : DependencyObject { for (int i = 0; i &amp;lt; VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null &amp;amp;&amp;amp; child is T) return (T)child; else { T childOfChild = FindVisualChild&amp;lt;T&amp;gt;(child); if (childOfChild != null) return childOfChild; } } return null; }&lt;/pre&gt;</pre></pre>
.
Take a look atObservableCollection
, you can use this to bind.
Changes work both ways, and it's easy to get to the values, in the exampleMyList
:
Code:
ObservableCollection<MyClass> MyList= new ObservableCollection<MyClass>();
Form:
<ListBox ItemsSource="{Binding MyList}"></ListBox>
More information can be found here: How to: Create and Bind to an ObservableCollection[^]
这篇关于如何在WPF中从datagrid保存数据列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!