|
Posted on
2008-08-15 08:49
Anna Yang
阅读( 341)
评论()
编辑
收藏
举报
有关ListView的操作
- 1.将数据库中的数据导入listview中
- 先写出将数据导入Listview的函数:
-
public static void FillListView(ListView lv, DataTable dt)
{
ListViewItem lvi;
lv.Items.Clear();
lv.Columns.Clear();
for (int i = 0; i < dt.Columns.Count; i++)
{
lv.Columns.Add(dt.Columns[i].Caption.ToString());
}
foreach (DataRow dr in dt.Rows)
{
lvi = lv.Items.Add(dr[0].ToString());
for (int i = 1; i < dt.Columns.Count; i++)
{
lvi.SubItems.Add(dr[i].ToString());
}
}
lv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
-
- 然后调用此函数将数据导入listview,在此dt的值为如下:
-
DataTable dt=new DataTable()
string strSQL = "select * from Routingline";
dt = DoDB.Execute(strSQL);
- 注意: 要将 listview属性中的“gridLines”的属性改为true.view的属性改为"details"
-
- 2.将listview中的数据显示在textbox中
-
private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtUserName.Text = this.dgvUser.CurrentRow.Cells[0].Value.ToString();
txtEnName.Text = this.dgvUser.CurrentRow.Cells[1].Value.ToString();
txtChName.Text = this.dgvUser.CurrentRow.Cells[2].Value.ToString();
}
-
|