vs2005怎么在C# 中将数据添加到listbox
3个回答
展开全部
一,for (int i = 0; i < 10; i++)
{
this.listBox1.Items.Add(i);
}
二,
this.listBox1.Items.AddRange(new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
{
this.listBox1.Items.Add(i);
}
二,
this.listBox1.Items.AddRange(new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵,没那么麻烦,点击listbox控件右上角的小三角,选择数据源,.net2005的优点就是减少编写代码量.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClassLibrary1.Access;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
ClassLibrary1.Access.AccessClass access = new AccessClass();
public Form1()
{
InitializeComponent();
}
public void list()
{
DataSet dataset = access.selectlist("");
foreach (DataRow row in dataset.Tables[0].Rows)
{
this.listView1.Items.Add(row[1].ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.list();
}
/// <summary>
/// 添加详细信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_Click(object sender, EventArgs e)
{
this.listView2.Items.Clear();
string listname = this.listView1.SelectedItems[0].Text.ToString();
DataSet dataset = new DataSet();
dataset = this.access.selectlist(listname);
for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
ListViewItem lstItem = new ListViewItem(dataset.Tables[0].Rows[i].ItemArray[0].ToString());
for (int j = 1;j<dataset.Tables[0].Columns.Count;j++)
{
lstItem.SubItems.Add(dataset.Tables[0].Rows[i].ItemArray[j].ToString());
}
this.listView2.Items.Add(lstItem);
}
//DataSet[] ds = new DataSet[2];
//ds[0] = this.access.selectlist(listname);
//for (int i = 0; i < ds[0].Tables[0].Rows.Count; i++)
//{
// ListViewItem lvItem = new ListViewItem(ds[0].Tables[0].Rows[i].ItemArray[0].ToString(), 1);
// this.listView2.Items.Add(lvItem);
// for (int j = 1; j < 6; j++)
// {
// lvItem.SubItems.Add(ds[0].Tables[0].Rows[i].ItemArray[j].ToString());
// }
//}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClassLibrary1.Access;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
ClassLibrary1.Access.AccessClass access = new AccessClass();
public Form1()
{
InitializeComponent();
}
public void list()
{
DataSet dataset = access.selectlist("");
foreach (DataRow row in dataset.Tables[0].Rows)
{
this.listView1.Items.Add(row[1].ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.list();
}
/// <summary>
/// 添加详细信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_Click(object sender, EventArgs e)
{
this.listView2.Items.Clear();
string listname = this.listView1.SelectedItems[0].Text.ToString();
DataSet dataset = new DataSet();
dataset = this.access.selectlist(listname);
for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
ListViewItem lstItem = new ListViewItem(dataset.Tables[0].Rows[i].ItemArray[0].ToString());
for (int j = 1;j<dataset.Tables[0].Columns.Count;j++)
{
lstItem.SubItems.Add(dataset.Tables[0].Rows[i].ItemArray[j].ToString());
}
this.listView2.Items.Add(lstItem);
}
//DataSet[] ds = new DataSet[2];
//ds[0] = this.access.selectlist(listname);
//for (int i = 0; i < ds[0].Tables[0].Rows.Count; i++)
//{
// ListViewItem lvItem = new ListViewItem(ds[0].Tables[0].Rows[i].ItemArray[0].ToString(), 1);
// this.listView2.Items.Add(lvItem);
// for (int j = 1; j < 6; j++)
// {
// lvItem.SubItems.Add(ds[0].Tables[0].Rows[i].ItemArray[j].ToString());
// }
//}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询