# 1. 首先写一个 List 转 DataTable 的方法

public DataTable ListToDt<T>(IEnumerable<T> collection)
{
  var props = typeof(T).GetProperties();
  var dt = new DataTable();
  dt.Columns.AddRange(props.Select(p => new
  DataColumn(p.Name, p.PropertyType)).ToArray());
  if (collection.Count() > 0)
  {
    for (int i = 0; i < collection.Count(); i++)
    {
      ArrayList tempList = new ArrayList();
      foreach (PropertyInfo pi in props)
      {
        object obj = pi.GetValue(collection.ElementAt(i), null);
        tempList.Add(obj);
      }
      object[] array = tempList.ToArray();
      dt.LoadDataRow(array, true);
    }
  }
  return dt;
}

# 然后定义一个 list

public List<Teacher> list = new List<Teacher>();
public class Teacher
{
    public string Name { get; set; }
    public string Password { get; set; }
}

# 方法内调用

DataTable dt = ListToDt(list);
DataView dv = new DataView(dt);

# 这样一个 List 就转成了 DataView

# 2. 然后是 DataTable 行转列的方法

private DataTable SwapTable(DataTable tableData)
{
  int intRows = tableData.Rows.Count;
  int intColumns = tableData.Columns.Count;
  // 转二维数组
  string[,] arrayData = new string[intRows, intColumns];
  for (int i = 0; i < intRows; i++)
  {
    for (int j = 0; j < intColumns; j++)
    {
      arrayData[i, j] = tableData.Rows[i][j].ToString();
    }
  }
  // 下标对换
  string[,] arrSwap = new string[intColumns, intRows];
  for (int m = 0; m < intColumns; m++)
  {
    for (int n = 0; n < intRows; n++)
    {
      arrSwap[m, n] = arrayData[n, m];
    }
  }
  DataTable dt = new DataTable();
  // 添加列
  for (int k = 0; k < intRows; k++)
  {
    dt.Columns.Add(new DataColumn(arrSwap[0, k]));
  }
  // 添加行
  for (int r = 1; r < intColumns; r++)
  {
    DataRow dr = dt.NewRow();
    for (int c = 0; c < intRows; c++)
    {
      dr[c] = arrSwap[r, c].ToString();
    }
    dt.Rows.Add(dr);
  }
  // 添加行头
  DataColumn ColRowHead = new DataColumn(tableData.Columns[0].ColumnName);
  dt.Columns.Add(ColRowHead);
  dt.Columns[ColRowHead.ColumnName].SetOrdinal(0);
  for (int i = 0; i < intColumns - 1; i++)
  {
    dt.Rows[i][ColRowHead.ColumnName] = tableData.Columns[i + 1].ColumnName;
  }
  return dt;
}

# 3. WPF 获取选中某行的值

/// <summary>
/// 选中事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
  DataRowView mySelectedItem = (DataRowView)dataGrid.SelectedItem;
  // 判断有没有选中
  if (mySelectedItem != null)
  {
    DataRow result = mySelectedItem.Row;
    DataTable dataTableNew = dataTable.Clone();
    dataTableNew.ImportRow(result);
    this.dataGrid1.ItemsSource = new DataView(dataTableNew);
  }
}

# 4. DataTable 筛选数据

DataRow[] dr = dataTable.Select("Name ='张三'", "Time DESC");
DataTable dataTableNew = dataTable.Clone();
for (int i = 0; i < dr.Length; i++)
{
  dataTableNew.ImportRow(dr[i]);
}
this.dataGrid1.ItemsSource = new DataView(dataTableNew);

# 5. TextBox 实时更新 Binding 的 Property

Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged}"

鸭梨好大

更新于 阅读次数

请我喝茶~( ̄▽ ̄)~*

Tianci 微信支付

微信支付

Tianci 支付宝

支付宝