### 封装方法
```C#
/// <summary>
/// DataTable转ListView
/// </summary>
/// <param name="lv"></param>
/// <param name="dt"></param>
public static void DataTabletoListView(System.Windows.Forms.ListView lv, System.Data.DataTable dt)
{
try
{
if (dt != null)
{
lv.View = System.Windows.Forms.View.Details;
lv.FullRowSelect = true;
lv.BeginUpdate();
lv.Clear();
lv.View = System.Windows.Forms.View.Details;
lv.AllowColumnReorder = true;
lv.GridLines = true;
lv.Items.Clear();
lv.Columns.Clear();
for (int i = 0; i < dt.Columns.Count; i++)
{
lv.Columns.Add(dt.Columns[i].Caption.ToString(), dt.Columns[i].Caption.Trim().Length * 20, System.Windows.Forms.HorizontalAlignment.Left);
}
foreach (System.Data.DaTarow dr in dt.Rows)
{
System.Windows.Forms.ListViewItem lvi = new System.Windows.Forms.ListViewItem(dr[0].ToString());
for (int j = 1; j < dt.Columns.Count; j++)
{
lvi.SubItems.Add(dr[j].ToString());
}
lv.Items.Add(lvi);
}
lv.AutoResizeColumns(System.Windows.Forms.ColumnHeaderAutoResizeStyle.HeaderSize);
lv.EndUpdate();
}
}
catch (Exception ex) { }
}
//网络素材仅限收藏 方便学习
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。