獲取DataGrid數據

Posted adoc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了獲取DataGrid數據相关的知识,希望对你有一定的参考价值。

//獲取清單

public DataTable getCheckList(DataGrid dgDetail, DataTable lo_DTabList, string row_name)
{
try
{
for (int i = 0; i < dgDetail.Items.Count; i++)
{
CheckBox lo_SelectCheckBoxInCell = dgDetail.Columns[0].GetCellContent(dgDetail.Items[i]) as CheckBox;
if (lo_SelectCheckBoxInCell != null)
{
if (lo_SelectCheckBoxInCell.IsChecked == true)
{
//1.整行抓取
//DataGridColumn dtcolumn = dgDetail.Columns[i];
//lo_DTabList.Rows.Add(dtcolumn);

DataRow dr = lo_DTabList.NewRow();
for (int j = 0; j < lo_DTabList.Columns.Count; j++)
{
//((dgDetail.Items[j]) as TextBlock).Text.ToString();
dr[j] = (dgDetail.Columns[j+1].GetCellContent(dgDetail.Items[i]) as TextBlock).Text.ToString();
}
lo_DTabList.Rows.Add(dr);

//string ls_row = ((dgDetail.Items[i]) as DataRowView)["emp"].ToString();
//2.單列抓取,需要傳入列名,如emp_no-> row_name
//string ls_row = ((dgDetail.Items[i]) as DataRowView)[row_name].ToString();
//string ls_EMP_NO = ((dgDetail.Items[i]) as DataRowView)["EMP_NO"].ToString();
//lo_DTabList.Rows.Add();
}
}
}
if (lo_DTabList.Rows.Count <= 0)
{
MessageBox.Show("請勾選要換崗的設備清單!", ls_TitleName, MessageBoxButton.OK, MessageBoxImage.Asterisk);
return null;
}
return lo_DTabList;
}
catch (Exception lo_Ex)
{
MessageBox.Show("getCheckList is Fail," + lo_Ex.Message.Substring(0, 200));
return null;
}
}

#region 獲取DataGrid 的表頭
public string[] getCellsDetail(DataGrid dgDetail)
{
try
{
string[] strPath=new string[dgDetail.Columns.Count];
string path = string.Empty;
for (int i = 0; i < dgDetail.Columns.Count; i++)
{
try
{
DataGridTextColumn dgcol = dgDetail.Columns[i] as DataGridTextColumn;
Binding binding = dgcol.Binding as Binding;
path = binding.Path.Path;
}
catch (Exception lo_ex)
{
//path = null;
continue;
}
strPath[i] = path;
}
return strPath;
}
catch (Exception lo_Ex)
{
MessageBox.Show("getCellsDetail is Fail," + lo_Ex.Message.Substring(0, 200));
return null;
}
}
#endregion

以上是关于獲取DataGrid數據的主要内容,如果未能解决你的问题,请参考以下文章

獲取串口數據,設置串口

Flutter dio獲取數據顯示到listview中

獲取12306所需剩餘的票

MVC+Ninject+三层架构+代码生成 -- 总结(數據層)

wpf datagrid怎么得到焦点?用datagrid.focus()不行

WPF关闭datagrid插入行