在wpf中使用listview [英] using of listview in wpf
问题描述
hi
我正在wpf(Windows应用程序)中使用C#.net
在该iam中,使用具有两列名称分别为item1和item2的列表视图,
我想根据条件从数据库中添加字段
我面临的问题是如何将其添加到listview
我找不到行和列之类的and选项,请帮助我
hi
i am using the C#.net in wpf(windows application)
in that iam using a list view with two columns name as item1 and item2 ,
i want to add the fields from the database basing on condition
i am facing the problem is how to add the it to the listview
i am not finding the and option like rows and columns please help me
<br />
i had created dynamically but every thing is running good but the at the listview display only one item is showing
OleDbConnection mcon = new OleDbConnection("provider =Microsoft.JET.OLEDB.4.0;data source=D:\\invoice\\invoice.mdb");
OleDbCommand da = new OleDbCommand("select * from invoicetable where invoiceno=''" + invoicenoxearchtxt.Text + "''order by ID asc", mcon);
OleDbDataReader dr;
mcon.Open();
dr = da.ExecuteReader();
ListItem la = new ListItem();
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("dt");
dt.Columns.Add("companyname", typeof(System.String));
dt.Columns.Add("customername", typeof(System.String));
// Create the GridView
if (dr.Read())
{
DataRow dr1 = dt.NewRow();
dr1["companyname"] = dr["companyname"].ToString();
dr1["customername"] = dr["customername"].ToString();
dt.Rows.Add(dr1);
//_searchcollection.Add(new searchdata
//{
// companyname = dr["companyname"].ToString(),
// customername = dr["customername"].ToString()
//});
}
// Create the GridView Columns
GridView gv = new GridView();
// Setup the GridView Columns
foreach (DataColumn item in ds.Tables[0].Columns)
{
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding(item.ColumnName);
gvc.Header = item.ColumnName;
gvc.Width = 150f;
gv.Columns.Add(gvc);
}
listView1.View = gv;
// Display the Data
listView1.DataContext = ds.Tables["dt"].DefaultView;
// listView1.Items.Add(_searchcollection);
// listView1.DataContext = ds.Tables[0].DefaultView;
// ListViewEmployeeDetails.DataContext = ds.Tables[0].DefaultView;
// listView1.Items.Add(_searchcollection);
mcon.Close();
我还用xaml创建了列,并将bindeb绑定到公司名称和客户名称
在Xamal中进行编码:请注意,这里我删除了<>"
i had also created the columns in xaml and i had bindeb to the companyname and customername
coding in xamal: note here i had removes the "<>"
ListView Canvas.Left="433" Canvas.Top="147" Height="250" <br />
Name="listView1" Widt h="317.826" SelectionMode="Single" ItemsSource="{Binding}" <br />
listview<br />
<br />
请推荐我该怎么做
预先感谢q
please sugesst me how to do this thing
thank q in advance
推荐答案
您需要使用Loop为ListView创建列.
您可以检查
http://social.msdn.microsoft.com/论坛/en-US/wpf/thread/5e2159f6-032b-4601-9e1e-ee403af3ad7 [
You need to use Loop to create columns for the ListView.
You can check
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5e2159f6-032b-4601-9e1e-ee403af3ad7[^]
Here the Columns are created dynamically, so that when the datatable is bound you need to recreate columns totally.
:rose:
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("dt");
dt.Columns.Add("companyname", typeof(System.String));
dt.Columns.Add("customername", typeof(System.String));
while (dr.Read())
{
DataRow dr1 = dt.NewRow();
dr1["companyname"] = dr["companyname"].ToString();
dr1["customername"] = dr["customername"].ToString();
dt.Rows.Add(dr1);
}
GridView gv = new GridView();
foreach (DataColumn item in ds.Tables[0].Columns)
{
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding(item.ColumnName);
gvc.Header = item.ColumnName;
gvc.Width = 150f;
gv.Columns.Add(gvc);
}
listView1.View = gv;
listView1.DataContext = ds.Tables["dt"].DefaultView;
谢谢.我的问题已得到解决,这是因为无需使用XAML就可以动态地提高人们的注意力
THANK Q MY PROBLEM IS SOLVED THANKQ FOR THE ATTENTION IT WAS CREATED DYNAMICALLY WITH OUT USING XAML
这篇关于在wpf中使用listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!