我想用SqlDataAdapter去填充listView1中的各项,应该怎么实现?
这是我已经写好的代码,下面该怎么做?
希望高手指教,谢谢!!!
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandText = "select * from song_table where song_name="+txt_ch_name .Text ;
cmd1.Connection = strconn.conn;
SqlDataAdapter dr = new SqlDataAdapter(cmd1 );

解决方案 »

  1.   

    listView要加很麻烦的,加列再加项,一行一行的~~
      

  2.   

    那ListView中只有用用数组来填充吗?
      

  3.   

    或者直接返回ILIST,然后foreach加或者数据源是ILIST
    DataSet也行
      

  4.   

    DATAGRIDVIEW 不是可以?
    用DATASET 循环弄?
    那就不如直接绑定到DATAGRIDVIEW
      

  5.   

    ListView好像不可以从设置数据源吧,大哥些,可以说具体点吗?
      

  6.   

    ListView自己去一行行的的绑定吧。
      

  7.   

    首先,我们不考虑什么控件
    ListView可以干什么?它是由什么组成的,好像他有很多ListViewItem组成的吧
    那么我什么都不考虑,如果返回一个Ilist<T>类型的数据,那么我是不是可以处理?
    IList<T> lst = GetData();
    ListView lv = new ListView();
    foreach (T t in lst)
    {
        lv.Add(new ListViewItem(t.value));//把T里面的某个字段放进去
    }
    这样不就可以了么其次,如果是使用其他方法,好像也是可以的吧,我无论怎么搞,我只要能获取到数据,好像就可以放入到ListView里面的吧?最后,建议自己以后使用某个东西的时候,看下当前控件或者类是怎么回事,有哪些特性
      

  8.   

    楼上这位大哥,真像一个老师,呵呵!!!不过我还是不怎么懂
    其实就是想把从数据库中的查询结果,是一个数据集合,包括不止一行数据,把它放入ListView中显示
    楼上这位大哥,你的建议很好,谢谢。
    不过,我还是不懂,你的IList<T>lst=GetData();是什么意思呀,我是新手不懂,多希望你说具体点!
      

  9.   

    IList<T> 是C#中的泛型  
    IList<T> lst=GetData() 就是GetData()这个方法返回一个泛型列表 T可以放入你的model类
    也可以这样绑定        IList<T> list = new List<T>();
            ListView listView1 = new ListView();
            listView1.DataSource = list;
            listView1.DataBind();
      

  10.   

    我只是举个例子,并且那个例子是可行的,T是泛型,里面可以填写你需要的类型,比如一个对象或者string,或者其他的都行。获取Ilist<T>需要使用SQLDATAReader,但其他也是可以的,具体我没试验,但我写的这个方法肯定是可以通过的。
      

  11.   

    listview 好像不可以完全把你的数据表中的内容显示出来的。
    如果硬要填充的话:
    方法一:
      SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds, "stu_tb");
                string[] str = new string[3];
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    str[0] = row["stuID"].ToString();
                    str[1] = row["name"].ToString();
                    str[2] = row["age"].ToString();
                    ListViewItem item = new ListViewItem(str);                this.listView1.Items.Add(item);
                }
       方法二:
      conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ListViewItem items = new ListViewItem();
                    string[] test = new string[] { reader["stuID"].ToString(), reader["name"].ToString(), reader["age"].ToString() };
                    items.SubItems.AddRange(test);                this.listView1.Items.Add(items);
                   
                }
                reader.Close();
                conn.Close();
    楼主试试代码,我搞了好久,好像还是不怎么行。
      

  12.   

    哎,我没有什么办法,我该用SqlDataReader了
    就是新建一个数组,Read方法读出来,填充数组
    最后在去填充ListView中的各项就是了
    下面是我程序中的代码,虽然显得有点笨,不过易懂实用!!!
    while (read.Read())
               {
                   array[0] = read.GetSqlInt32(0).ToString ();
                   array[1] = read.GetSqlInt32(1).ToString ();
                   array[2]=  read. GetSqlString(2).ToString ();
                   array[3] = read.GetSqlString(3).ToString ();
                   array[4] = read.GetSqlString(4).ToString ();
                   array[5] = read.GetSqlString(5).ToString ();
                   array[6] = read.GetSqlString(6).ToString ();
                   array[7] = read.GetSqlInt32(7).ToString();
                   this.listView2.Items.Add(new ListViewItem(array ));
               }
               read.Close();
      

  13.   

    方法大同小异,用我上面提供的SqlDataAdapter那个方法也是一样的。