listview绑定数据源,怎么向listview中写入数据,不是绑定数据
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
要对listview 设置以下属性:
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.GridLines = true;
//测试代码:
private void button1_Click(object sender, EventArgs e)
{
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.GridLines = true;
int cValue = (int)'A';
string str = "";
for (int i = 0; i < 5; i++)
{
cValue += 1;
ListViewItem item = new ListViewItem();
str = ((char)cValue).ToString();
item.Text = str;
item.SubItems.Add(str + i.ToString());
this.listView1.Items.Add(item);
}
}
/// <summary>
/// 为ListView绑定DataTable数据项
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="lv">ListView控件</param>
static public void dataTableToListview(DataTable dt, ListView lv)
{
if (dt != null)
{
lv.View = View.Details;
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());//增加标题
}
for (int i = 0; i < dt.Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem(dt.Rows[i][0].ToString());
for (int j = 0; j < dt.Columns.Count; j++)
{
// lvi.ImageIndex = 0;
lvi.SubItems.Add(dt.Rows[i][j].ToString());
}
lv.Items.Add(lvi);
}
lv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);//调整列的宽度
}
}
/// <summary>
/// ListView反向填充DataTable数据项
/// </summary>
/// <param name="lv">ListView控件</param>
/// <param name="dt">DataTable</param>
static public void listViewToDataTable(ListView lv, DataTable dt)
{
DataRow dr;
dt.Clear();
dt.Columns.Clear();
for (int k = 0; k < lv.Columns.Count; k++)
{
dt.Columns.Add(lv.Columns[k].Text.Trim().ToString());//生成DataTable列头
}
for (int i = 0; i < lv.Items.Count; i++)
{
dr = dt.NewRow();
for (int j = 0; j < lv.Columns.Count; j++)
{
dr[j] = lv.Items[i].SubItems[j].Text.Trim();
}
dt.Rows.Add(dr);//每行内容
}
}
}
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.GridLines = true;
//测试代码:
private void button1_Click(object sender, EventArgs e)
{
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.GridLines = true;
int cValue = (int)'A';
string str = "";
for (int i = 0; i < 5; i++)
{
cValue += 1;
ListViewItem item = new ListViewItem();
str = ((char)cValue).ToString();
item.Text = str;
item.SubItems.Add(str + i.ToString());
this.listView1.Items.Add(item);
}
}
/// <summary>
/// 为ListView绑定DataTable数据项
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="lv">ListView控件</param>
static public void dataTableToListview(DataTable dt, ListView lv)
{
if (dt != null)
{
lv.View = View.Details;
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());//增加标题
}
for (int i = 0; i < dt.Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem(dt.Rows[i][0].ToString());
for (int j = 0; j < dt.Columns.Count; j++)
{
// lvi.ImageIndex = 0;
lvi.SubItems.Add(dt.Rows[i][j].ToString());
}
lv.Items.Add(lvi);
}
lv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);//调整列的宽度
}
}
/// <summary>
/// ListView反向填充DataTable数据项
/// </summary>
/// <param name="lv">ListView控件</param>
/// <param name="dt">DataTable</param>
static public void listViewToDataTable(ListView lv, DataTable dt)
{
DataRow dr;
dt.Clear();
dt.Columns.Clear();
for (int k = 0; k < lv.Columns.Count; k++)
{
dt.Columns.Add(lv.Columns[k].Text.Trim().ToString());//生成DataTable列头
}
for (int i = 0; i < lv.Items.Count; i++)
{
dr = dt.NewRow();
for (int j = 0; j < lv.Columns.Count; j++)
{
dr[j] = lv.Items[i].SubItems[j].Text.Trim();
}
dt.Rows.Add(dr);//每行内容
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询