我想用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 );
这是我已经写好的代码,下面该怎么做?
希望高手指教,谢谢!!!
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 );
解决方案 »
- C# 数据库
- location.href 引导到页面后,IsPostBack为真?
- 问个硬件问题,我的电脑开机后,不自检,没有滴的一声,也不报警,显示器无信号,只有风扇在转,只有重起或重新开机N次才偶尔进去,但进到系统后,不过5分钟就死机了,哪位高手遇到过啊,指点一下,快崩溃了 !
- datetime类型
- Exception Application ::Run+0xf StartForm ::Main+0xa
- c#窗体程序主窗口中tabPage中显示子窗口中控件tabPage不能显示第二页
- 设计实体类问题:
- 什么是“windows用户体验”?
- 100分求解,遇到一个极度奇怪的问题~~~!!!
- 用C#写的程序可以脱离.net框架运行吗?
- 为什么添加本地化会影响窗口的布局?
- C# 泛型方法问题
DataSet也行
用DATASET 循环弄?
那就不如直接绑定到DATAGRIDVIEW
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里面的吧?最后,建议自己以后使用某个东西的时候,看下当前控件或者类是怎么回事,有哪些特性
其实就是想把从数据库中的查询结果,是一个数据集合,包括不止一行数据,把它放入ListView中显示
楼上这位大哥,你的建议很好,谢谢。
不过,我还是不懂,你的IList<T>lst=GetData();是什么意思呀,我是新手不懂,多希望你说具体点!
IList<T> lst=GetData() 就是GetData()这个方法返回一个泛型列表 T可以放入你的model类
也可以这样绑定 IList<T> list = new List<T>();
ListView listView1 = new ListView();
listView1.DataSource = list;
listView1.DataBind();
如果硬要填充的话:
方法一:
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();
楼主试试代码,我搞了好久,好像还是不怎么行。
就是新建一个数组,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();