目录
Wpf绑定Datagrid用datatable数据
GridView获取当前行
wpf获取当前行的某列数值
Wpf获取当前的总行数(删除多行)
Wpf绑定Datagrid用datatable数据
string sql = "select id as 编号,name as 花名,price AS 价格,number as 库存 from t_flower";
var a = DBHlper.GetDataTable(sql);
dataGrid1.ItemsSource = a.DefaultView;
GridView获取当前行
var selectedRow = dd.SelectedItem; ;
var a = this.dd.SelectedItem;
var b = a as DataRowView;
if (b == null)
{
return;
}
MessageBox.Show("您选择删除的书籍是" + b[1].ToString());
string did = b[0].ToString();
string sql = ($"update [dbo].[net] set 状态='{com.Text}' where id={com.SelectedIndex + 1}");
if (dbhelper.ExecuteNonQuery(sql))
{
MessageBox.Show("拉黑成功", "tip", MessageBoxButton.OK);
string sql1 = "select * from book";
dd.ItemsSource = dbhelper.GetDataTable(sql1).DefaultView;
}
else
{
MessageBox.Show("拉黑失败", "tip");
}
wpf获取当前行的某列数值
var item = dataGrid1.SelectedItem as DataRowView;
if (item != null)
{
var selectedRow = dataGrid1.SelectedItem as DataRowView;
string id = selectedRow["编号"].ToString();
}
Wpf获取当前的总行数(删除多行)
int n = dataGrid1.SelectedItems.Count;
DataRowView[] drv = new DataRowView[n];
for (int i = 0; i < n; i++)
{
drv[i] = dataGrid1.SelectedItems[i] as DataRowView;
}
string sql = $"delete from t_flower where id in (";
for (int i = 0; i < n; i++)
{
sql += $"'{drv[i][0]}',";
}
sql = sql.Remove(sql.Length - 1);//删除最后一个字符
sql += ")";
Dao dao = new Dao();
if (dao.Execute(sql) > n - 1)
{
MessageBox.Show($"成功删除{n}条花束信息");
Table();
}