日期:2014-05-18  浏览次数:20924 次

winform下listview
dataset中数据怎么显示到listview中
 string strSQL = " select address,name from s ";
  myConn.Open();
  SqlCommand myCommand = new SqlCommand(strSQL, myConn);
  SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
  DataSet ds = new DataSet();
  adapter.Fill(ds);
   



下面怎么写??

------解决方案--------------------
private void fillListView(DataSet DS, ListView lv, int table)
{//dataset绑定listview函数
DataTable dt = DS.Tables[table];
int fc = dt.Columns.Count;
 
lv.Columns.Clear();
lv.Items.Clear();

foreach (DataRow row in dt.Rows)
{
string[] subitems = new string[fc];
 
object[] o = row.ItemArray;
for (int i = 0; i < fc; i++)
{
subitems[i] = o[i].ToString();
}
ListViewItem item = new ListViewItem(subitems);
lv.Items.Add(item);
}
}
另外,你的sql语句有问题
 string strSQL = " select address,name from s ";红色部分好像是中文逗号
------解决方案--------------------
试试这个:
string strSQL = " select address,name from s ";
myConn.Open();
SqlCommand myCommand = new SqlCommand(strSQL, myConn);
SqlDataReader reader=myCommand.ExecuteReader();
listView1.Items.Clear();
while(reader.Read())
{
ListViewItem lvi = new ListViewItem(reader["address"].ToString());
lvi.SubItems.Add(reader["name"].ToString());//根据listview1中对应的列名进行排列

listView1.Items.Add(lvi);
}
myConn.Close();