查看: 928|回复: 8
|
怎樣才可以把data分類在list view裡
[复制链接]
|
|
我做了個list view﹐裡面有四個column
這是用C#寫的
以下是我的寫法﹕
MyItem = ListViewBookRecord.Items.Add( ( ( dr[ "ID".ToString() ] ) ) );
MyItem.SubItems.Add( dr[ "bookname".ToString() ] );
MyItem.SubItems.Add( dr[ "authorname".ToString() ] );
MyItem.SubItems.Add( dr[ "booktype".ToString() ] );
MyItem.SubItems.Add( dr[ "price".ToString() ] );
但出了error:
The name 'MyItem' does not exist in the class or namespace‘bookshop’
和
The type or namespace name 'MyItem' could not be found (are you missing a using directive or an assembly reference?)
我的data是從database裡拿的
想到我頭都大賣和用過不同的方式去些都不可以
請問應該怎樣寫才能讓data分別放在不同的column呢﹖
謝謝
[ 本帖最后由 lieweffect 于 14-6-2006 08:46 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 14-6-2006 11:11 PM
|
显示全部楼层
MyItem = ListViewBookRecord.Items.Add( ( ( dr[ "ID".ToString() ] ) ) );
MyItem.SubItems.Add( dr[ "bookname".ToString() ] );
MyItem.SubItems.Add( dr[ "authorname".ToString() ] );
MyItem.SubItems.Add( dr[ "booktype".ToString() ] );
MyItem.SubItems.Add( dr[ "price".ToString() ] );
不是真的了解C#不过试试这个
MyItem = ListViewBookRecord.Items.Add( ( ( dr[ "ID".ToString() ] ) ) );
ListViewBookRecord.SubItems.Add( dr[ "bookname".ToString() ] );
ListViewBookRecord.SubItems.Add( dr[ "authorname".ToString() ] );
ListViewBookRecord.SubItems.Add( dr[ "booktype".ToString() ] );
ListViewBookRecord.SubItems.Add( dr[ "price".ToString() ] );
|
|
|
|
|
|
|
|
发表于 15-6-2006 02:08 PM
|
显示全部楼层
string[] sData = new string[3];
SqlDataReader reader = sqlcom.ExecuteReader();
while(reader.read())
{
sData[0] = reader.GetString(0); //using sqldatareader
sData[1] = reader.GetInt32(1);
sData[2] = reader.GetString(2);
}
reader.close();
ListviewItem li = new ListviewItem(sData);
listView.items.add(li);
注意大小写
[ 本帖最后由 ctrl 于 15-6-2006 02:09 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 15-6-2006 07:38 PM
|
显示全部楼层
原帖由 ctrl 于 15-6-2006 02:08 PM 发表
string;
SqlDataReader reader = sqlcom.ExecuteReader();
while(reader.read())
{
sData = reader.GetString(0); //using sqldatareader
sData = reader.GetInt32(1);
sData = reader.GetString(2);
}
...
我的原本是這樣的
public void FillListBox( string Sqlstring )
{
ListViewBookRecord.Items.Clear();
// Declare Database Connection,Database Command, Dataset and Data Adapter
// Initialize the Database, Dataset and Data Connection
OleDbConnection Conn = new OleDbConnection( ConnString );
Conn.Open();
OleDbDataReader dr;
OleDbCommand dbcom= new OleDbCommand();
dbcom= new OleDbCommand();
DataSet ds = new DataSet();
ds= new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand( Sqlstring, Conn );
da.Fill( ds );
dbcom.Connection = ( Conn );
dbcom.CommandText = Sqlstring;
dr = dbcom.ExecuteReader();
// Fill the list box with the Book ID
while ( dr.Read() )
{ ListViewBookRecord.Items.Add(( dr["ID"].ToString()));
ListViewBookRecord.SubItems.Add(( dr["bookname"].ToString()));
ListViewBookRecord.SubItems.Add(( dr["authorname"].ToString()));
ListViewBookRecord.SubItems.Add(( dr["booktype"].ToString()));
ListViewBookRecord.SubItems.Add(( dr["price"].ToString()));
}
dr.Close();
Conn.Close();
Conn.Dispose();
return;
}
我的是list了之後才close data reader
不是很明白為什麼close了data reader之後才list
請問到底要怎麼改呢﹖
謝謝 |
|
|
|
|
|
|
|
发表于 15-6-2006 10:08 PM
|
显示全部楼层
原帖由 lieweffect 于 14-6-2006 08:43 PM 发表
但出了error:
The name 'MyItem' does not exist in the class or namespace‘bookshop’
和
The type or namespace name 'MyItem' could not be found (are you missing a using directive or an assembly reference?)
这 error message 的意思是 MyItem 并没有被 declare, 所以 .Net 不知道 MyItem 是什么... |
|
|
|
|
|
|
|
发表于 16-6-2006 09:27 AM
|
显示全部楼层
原帖由 lieweffect 于 15-6-2006 07:38 PM 发表
我的原本是這樣的
public void FillListBox( string Sqlstring )
{
ListViewBookRecord.Items.Clear();
// Declare Database Connection,Database Command, Dataset a ...
谢谢提醒
忘了放在里面
跟着我的方法做一定做到
我用了很多次了 |
|
|
|
|
|
|
|

楼主 |
发表于 17-6-2006 11:12 AM
|
显示全部楼层
原帖由 ctrl 于 16-6-2006 09:27 AM 发表
谢谢提醒
忘了放在里面
跟着我的方法做一定做到
我用了很多次了
還是不行
我用try catch
它說:
The data value could not be converted for reasons other than sign mismatch or data overflow.
要怎樣呢﹖
[ 本帖最后由 lieweffect 于 17-6-2006 11:20 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 19-6-2006 06:40 PM
|
显示全部楼层
原帖由 lieweffect 于 17-6-2006 11:12 AM 发表
還是不行
我用try catch
它說:
The data value could not be converted for reasons other than sign mismatch or data overflow.
要怎樣呢﹖
你的sqlstring是怎样写?
你可以试试用Convert
example:
let's say sqlstring = select ID, bookname, authorname,booktype, price from testing
while(dr.read())
{
string[] sdata = new string[5];
sdata[0] = convert.tostring(dr.getint32(0));
sdata[1] = dr.getstring(1);
sdata[2] = dr.getstring(2);
sdata[3] = dt.getstring(3);
sdata[4] = convert.tostring(dr.getsingle(4));
listviewitem li = new listviewitem(sdata);
listview.items.add(li);
}
dr.close(); |
|
|
|
|
|
|
|

楼主 |
发表于 19-6-2006 07:19 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|