不知道你的DataBase是什么,怎么从DataBase取数据到DataTable就不说了
/// <summary>
/// Fill listview with data table
/// </summary>
/// <param name="lv">Listview</param>
/// <param name="dt">Data table</param>
public static void FillListView(ListView lv, DataTable dt)
{
ListViewItem lvi;
lvItemsClear();
lvColumnsClear();
for (int i = 0; i < dtColumnsCount; i++)
{
lvColumnsAdd(dtColumns[i]CaptionToString());
}
foreach (DataRow dr in dtRows)
{
lvi = lvItemsAdd(dr[0]ToString());
for (int i = 1; i < dtColumnsCount; i++)
{
lviSubItemsAdd(dr[i]ToString());
if (lvCheckBoxes == true && dr[i]ToString() == "Has")
{
lviChecked = true;
}
}
}
lvAutoResizeColumns(ColumnHeaderAutoResizeStyleHeaderSize);
}
封装数据库字段
namespace ShopModel{
public class Shops
{
string shopname;
double Price;
int number;
int allnumber;
int singlenumber;
public string Shopname
{
get { return shopname; }
set { shopname = value; }
}
public double Price1
{
get { return Price; }
set { Price = value; }
}
public int Number
{
get { return number; }
set { number = value; }
}
public int Allnumber
{
get { return allnumber; }
set { allnumber = value; }
}
public int Singlenumber
{
get { return singlenumber; }
set { singlenumber = value; }
}
}
}
查询所有数据
private static IList<Shops> Getnumber()
{
string sql = "select from Shop";
List<Shops> list = new List<Shops>();
using(DataTable dt = DBHelperGetDataSet(sql))
{
foreach(DataRow row in dtRows )
{
Shops shop = new Shops();
shopShopname =(string)row["SpName"];
shopNumber =(int)row["SpNumber"];
shopPrice1 = ConvertToDouble( row["SpPrice"]);
shopAllnumber = (int)row["SpAllnumber"];
shopSinglenumber = (int)row["Spsinglenumber"];
listAdd(shop);
}
return list;
}
}
写个方法把数据显示到listview
public void GetRecordBytyep(){
IList<Records> ilist = RecordMangerGetRecodbytype();
foreach(Records rd in ilist)
{
ListViewItem lvitem = new ListViewItem(); //这个 new 必须放在 遍历里面
lvitemSubItemsClear();
lvitemSubItems[0]Text = rdRecordidToString();
lvitemSubItemsAdd(rdPcnumberToString() + "号");
lvitemSubItemsAdd(rdMoneyToString() + "元");
lvitemSubItemsAdd( stringFormat("{0:yyyy-MM-dd HH:mm}", rdPcontime) );
lvitemSubItemsAdd(stringFormat("{0:yyyy-MM-dd HH:mm}", rdPcdowntime));
listView1ItemsAdd(lvitem);
}
}
刚好做了个小程序,复制一下代码给你参考一下,用到了三层架构。
不用ADO 那就用RDO DAO也可
反正总要调用个数据库引擎 你才能使用里面的数据库
给你一段用ADO的代码 希望你能看懂
Dim r As New ADODBRecordset
Dim SQL As String
Dim itmx As ListItem
ListView1ListItemsClear
SQL = "SELECT 报告idd, 报告ID, 报告姓名, 报告性别, 报告年龄, 报告放入日期, "
SQL = SQL & " 报告结论, 报告结论日期, 报告明细reidd, 报告明细名称, 报告明细条码,"
SQL = SQL & "报告明细放入时间, 报告明细放入人员, 报告明细取出时间, 报告明细取出人员, 报告明细时间间隔, 报告明细结果, 报告明细说明, 报告明细位置 "
SQL = SQL & "FROM 报告, 报告明细 where 报告idd = 报告明细reidd "
SQL = SQL & " and 报告明细放入时间>=#" & Format(RQ1, "yyyy-MM-dd") & "# "
SQL = SQL & " and 报告明细放入时间<#" & Format(RQ2 + 1, "yyyy-MM-dd") & "# "
rOpen SQL, cn
Do While Not rEOF
Set itmx = ListView1ListItemsAdd
itmxText = Trim(r!idd & " ")
itmxSubItems(1) = Trim(r!id & " ")
itmxSubItems(2) = Trim(r!姓名 & " ")
itmxSubItems(3) = Trim(r!性别 & " ")
itmxSubItems(4) = Trim(r!年龄 & " ")
itmxSubItems(5) = Format(r!放入日期, "yyyy-MM-dd")
itmxSubItems(6) = Trim(r!名称 & " ")
itmxSubItems(7) = Trim(r!条码 & " ")
itmxSubItems(8) = Format(r!放入时间, "yyyy-MM-dd hh:mm:ss")
itmxSubItems(9) = Trim(r!放入人员 & " ")
itmxSubItems(10) = Format(r!取出时间, "yyyy-MM-dd hh:mm:ss")
itmxSubItems(11) = Trim(r!取出人员 & " ")
itmxSubItems(12) = Format(r!时间间隔 / 24, "00")
itmxSubItems(13) = Trim(r!结果 & " ") & Trim(r!说明 & " ")
itmxSubItems(14) = Trim(r!位置 & " ")
rMoveNext
Loop
rClose
如下:因为一行显示4个,所以:GroupItemCount="4"
LayoutTemplate里放置一个table, GroupTemplate里放置一个tr,ItemTemplate里放置一个td
<asp:ListView ID="ListView1" runat="server" GroupItemCount="4">
<LayoutTemplate>
<table id="groupPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td runat="server" >
<img src=<%# Eval("img") % >
<br />
<img src=<%# Eval("bookname") % />
</td>
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="12">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
你listview肯定写个adapter,如果自己重写adapter 写个setlist的方法,获取所有数据后直接setlist,然后在adapter里面的getview里面吧setlist里面的list赋值给view,然后在activity里面这个adapternotifyDataSetChanged()
这是我原来重写的adapter
package comliandisysKohlerVabactivitymusic;
import javautilList;
import androidcontentContext;
import androidgraphicsTypeface;
import androidviewLayoutInflater;
import androidviewView;
import androidviewViewGroup;
import androidwidgetBaseAdapter;
import androidwidgetTextView;
import comliandisysKohlerVabactivityR;
import comliandisysKohlerVabbeanMusicContentBean;
/
重写adapt
/
public class EditMusicListAdapt extends BaseAdapter {
private class GridHolder {
TextView appName;
}
private GridHolder holder;
private Context context;
private List<> list;
private LayoutInflater mInflater;
private Typeface tf;
//
// private int[] imgages = { Rdrawablemenu_ting_on,
// Rdrawablemenu_xin_on,
// Rdrawablemenu_x_on, Rdrawablemenu_wjj_on,
// Rdrawablemenu_jia_on, Rdrawablemenu_ban_on };
// 更新提示的
// private int[] imgages2 = { Rdrawablemenu_ting_new_on,
// Rdrawablemenu_xin_new_on, Rdrawablemenu_x_new_on,
// Rdrawablemenu_wjj_new_on, Rdrawablemenu_jia_new_on,
// Rdrawablemenu_ban_new_on };
public EditMusicListAdapt(Context c) {
thiscontext = c;
}
public EditMusicListAdapt(Context c , Typeface tf) {
thiscontext = c;
thistf = tf;
}
public void setList(List<> list) {
thislist = list;
mInflater = (LayoutInflater) context
getSystemService(ContextLAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return listsize();
}
@Override
public Object getItem(int index) {
return listget(index);
}
//
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View convertView, ViewGroup parent) {
convertView = mInflaterinflate(Rlayoutmusic_list_item, null);
holder = new GridHolder();
holderappName = (TextView) convertView
findViewById(Rmusic_list_itemtextview);
if (listget(index) instanceof MusicContentBean) {
MusicContentBean musicPlaylistBean = (MusicContentBean) list
get(index);
holderappNamesetText(musicPlaylistBeangetMusic_name());
if(!musicPlaylistBeanisClick_flag()){
holderappNamesetBackgroundResource(Rdrawablemusic_song);
}else{
holderappNamesetBackgroundResource(Rdrawablemusic_song_hover);
}
holderappNamesetTextSize(13);
holderappNamesetTypeface(tf);
}
convertViewsetTag(holder);
return convertView;
}
}
'省略打开数据库连接
Dim rs As New ADODBRecordset
Dim strsql as string
strsql="Select mz, xb Form 1234"
rsOpen strsql, dbConnection, adOpenKeyset, adLockReadOnly
Dim xitem As ListItem
While Not rsEOF
Set xitem = ListView1ListItemsAdd( , , rsFields("mz"))
xitemSubItems("1") = rsFields("xb")
rsMoveNext
Wend
Set xitem = Nothing
rsClose
Set rs = Nothing
用代码慢慢加吧!假设ListView上的列已经全部建立,按次序对应Query里的所有记录,而且所有的字段值都可以用QueryFields
AsString来表示就可以了,那么代码可以如下
nFieldIndex
:
Integer
;
nItem:
TListItem
;begin
queryFiest
;
while
not
queryEof
do
begin
nItem
ListViewItemsAdd
;
nItemCaption
queryFields
AsString
;
for
nFieldIndex
1
to
queryFieldsCount-1
do
nItemSubItemsAdd(queryFields
AsString)
;
end
;end
;
以上就是关于C#中listview如何加载数据库数据全部的内容,包括:C#中listview如何加载数据库数据、c#中如何将在数据库查询的数据在listview控件中显示出来、VB listview 如何显示SQL 数据库谢谢!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)