WPF ListView实现编辑删除功能
我想实现ListView的编辑删除功能,首先绑定listview的数据privatevoidBindListView(){DBHandlerdbh=GetHandler....
我想实现ListView的编辑删除功能,
首先绑定listview的数据
private void BindListView()
{
DBHandler dbh = GetHandler.Init();
dbh.CommandText = "select * from commodity order by id desc";
DataTable commoditystable = dbh.ExecuteDataTable();
dbh.CommandText = "select * from smallclass_id";
DataTable smalltable = dbh.ExecuteDataTable();
dbh.CommandText = "select * from bigclass_id";
DataTable bigtable = dbh.ExecuteDataTable();
var newssound =
(from commoditys in commoditystable.AsEnumerable()
join small in smalltable.AsEnumerable() on
commoditys.Field<int>("small_id") equals
small.Field<int>("smallclassid")
join big in bigtable.AsEnumerable() on
commoditys.Field<int>("big_id") equals
big.Field<int>("bigclassid")
select new
{
id = commoditys.Field<int>("id"),
product_name = commoditys.Field<string>("product_name"),
price = commoditys.Field<float>("price"),
old_price = commoditys.Field<float>("old_price"),
stock = commoditys.Field<int>("stock"),
sell_count = commoditys.Field<int>("sell_count"),
product_ID = commoditys.Field<string>("product_ID"),
smallclassname = small.Field<string>("smallclassname"),
bigclassname = big.Field<string>("bigclassname")
}).ToList();
list.ItemsSource = newssound;
}
跟着在listview里面放置button按钮,
按钮事件为
private void del_Click(object sender, RoutedEventArgs e)
{
ListViewItem curItem = ((ListViewItem)list.ContainerFromElement((Button)sender));
var obj = curItem.Content;
DataRowView rv = obj as DataRowView;
}
这样得到的rv为空,请问怎么解决啊
obj获取的数据如图
数据如图 展开
首先绑定listview的数据
private void BindListView()
{
DBHandler dbh = GetHandler.Init();
dbh.CommandText = "select * from commodity order by id desc";
DataTable commoditystable = dbh.ExecuteDataTable();
dbh.CommandText = "select * from smallclass_id";
DataTable smalltable = dbh.ExecuteDataTable();
dbh.CommandText = "select * from bigclass_id";
DataTable bigtable = dbh.ExecuteDataTable();
var newssound =
(from commoditys in commoditystable.AsEnumerable()
join small in smalltable.AsEnumerable() on
commoditys.Field<int>("small_id") equals
small.Field<int>("smallclassid")
join big in bigtable.AsEnumerable() on
commoditys.Field<int>("big_id") equals
big.Field<int>("bigclassid")
select new
{
id = commoditys.Field<int>("id"),
product_name = commoditys.Field<string>("product_name"),
price = commoditys.Field<float>("price"),
old_price = commoditys.Field<float>("old_price"),
stock = commoditys.Field<int>("stock"),
sell_count = commoditys.Field<int>("sell_count"),
product_ID = commoditys.Field<string>("product_ID"),
smallclassname = small.Field<string>("smallclassname"),
bigclassname = big.Field<string>("bigclassname")
}).ToList();
list.ItemsSource = newssound;
}
跟着在listview里面放置button按钮,
按钮事件为
private void del_Click(object sender, RoutedEventArgs e)
{
ListViewItem curItem = ((ListViewItem)list.ContainerFromElement((Button)sender));
var obj = curItem.Content;
DataRowView rv = obj as DataRowView;
}
这样得到的rv为空,请问怎么解决啊
obj获取的数据如图
数据如图 展开
1个回答
展开全部
obj as DataRowView;
改成强制类型转换试一试?
(DataRowView)DataRowView
另外,调试单步到 curltem ,obj你看看这俩有值吗
改成强制类型转换试一试?
(DataRowView)DataRowView
另外,调试单步到 curltem ,obj你看看这俩有值吗
追问
curltem ,obj都有值
我觉得是linq
new
{
id = commoditys.Field("id"),
price = commoditys.Field("price"),
stock = commoditys.Field("stock"),
}
这个类型不能转换为DataRowView
请问是不是呢?
如果是要怎么转换呢??
追答
你是想实现当前行的编辑删除功能不是?
那我帮你获取行号,然后你通过行号来找主键,通过主键来操作数据表你看行吗?
全局变量:static string rowindex;//用来存放取出来的行号
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int length = listView1.SelectedItems.Count;
for (int i = 0; i < length; i++)
{
rowindex= (listView1.SelectedItems[i].Index + 1).ToString();
}
}
//拖拽一个button控件,激活Click事件
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Num); //我举一个你使用行号的例子
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询