关于wpf DataGrid 的增加删除行操作
来源:互联网 发布:网络阅读缺点 编辑:程序博客网 时间:2024/05/22 13:21
http://bbs.csdn.net/topics/390876617 原帖问题,居然回复不来了
最后 通过duanzi_peng 的建议 绑定了一个 数组 通过对数组的增加删 来实现 DataGrid的行数变化, 然后遍历出这个数组的 类,然后直接将这个类包含的值 插入数据库 下面是我的代码,需要的朋友可以参考下
[code=csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;
using HRMSys.Model;
using HRMSys.DAL;
using System.Collections;
using System.Data;
using System.ComponentModel;
namespace HRMSys.UI.Scanning
{
/// <summary>
/// FchengjianWindow.xaml 的交互逻辑
/// </summary>
public partial class FchengjianWindow : Window
{
public string EditingId { get; set; }
public ObservableCollection<Student> StuList { get; set; } //动态数组
public FchengjianWindow()
{
InitializeComponent();
StuList = new ObservableCollection<Student>();
this.DataContext = this;
grd.Focus();
}
public class Student : INotifyPropertyChanged
{
public int FId { get; set; }
public string InNumber{ get; set; }
#region 属性更改通知
public event PropertyChangedEventHandler PropertyChanged;
private void Changed(string PropertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
#endregion
}
private void grd_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter )
{
}
if (e.Key == Key.F5)
{
foreach (Student tq in StuList)
{
FinishJincang fc = new FinishJincang();
fc.RunNO = Convert.ToInt64(EditingId);//获得界面流程卡号
try
{
fc.InNumber = Convert.ToDouble(tq.InNumber);
if (fc.InNumber == 0)
{
MessageBox.Show("第 " + tq.FId + " 行输入的的米数有误,请重新输入");
return;
}
}
catch
{
MessageBox.Show("第 " + tq.FId + " 行输入的的米数有误,请重新输入");
return;
}
fc.Import = 1;
fc.Indate = System.DateTime.Now;
new FinishJincangDAL().Insert(fc);
}
System.Windows.Forms.MessageBox.Show("保存成功");
}
if (e.Key == Key.Back) //删除行
{
for (int i = 0; i < StuList.Count; i++)//不用for会报错
{
foreach (Student tq in StuList) //删除选中行
{
StuList.RemoveAt(tq.FId);
break;
}
}
}
}
private void grd_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
{
// 序列号
if (StuList == null || StuList.Count == 0)
{
((Student)e.NewItem).FId = 1;
}
else
{
((Student)e.NewItem).FId = StuList.Max(p => p.FId) + 1;
}
}
private void grd_Loaded(object sender, RoutedEventArgs e)
{ //加载的时候就控制焦点 在第一行
DataGridCell cell = GetCell(0, 1);
if (cell != null)
{
cell.Focus();
grd.BeginEdit();
}
}
/// <summary>
/// 根据行、列索引取的对应单元格对象
/// </summary>
/// <param name="row"></param>
/// <param name="column"></param>
/// <returns></returns>
public DataGridCell GetCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
grd.ScrollIntoView(rowContainer, grd.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
/// <summary>
/// 根据行索引取的行对象
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)grd.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
grd.ScrollIntoView(grd.Items[index]);
row = (DataGridRow)grd.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
/// <summary>
/// 获取指定类型的子元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parent"></param>
/// <returns></returns>
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
}
}
0 1
- 关于wpf DataGrid 的增加删除行操作
- 增加删除DataGrid行
- WPF DataGrid 如何对选择的多行进行删除
- DataGrid中数据的增加/删除/获取
- jquery easyui datagrid的增加,修改,删除
- WPF 删除DataGrid 行 弹出确认对话框
- DataGrid里嵌入checkBox,增加,删除等控件等操作
- 关于用jquery 动态增加、删除表格的行,列操作。
- WPF DataGrid 操作笔记
- dataGrid 控制增加新列 删除列的 东东!!
- WPF下给DataGrid自动增加序列号
- Jquery动态增加行和删除行的操作
- mysql命名行下对外键的增加删除操作
- 使用wpf 的datagrid 对选择的数据操作
- WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法
- [IOS]关于UITableView行的增加和删除
- WPF获取DataGrid单元格和获取DataGrid行的方法
- WPF DataGrid设置行的格式背景
- ExtJS学习----------Ext.DomHelper类学习(实例)
- thinkphp的getField方法
- Asynchronous Android读书笔记五Queuing Work with IntentService
- Fio IO性能测试工具
- TCP、UDP和HTTP的区别
- 关于wpf DataGrid 的增加删除行操作
- Cglib 字节码加强试用
- ssh开发中的DAO和DaoSupport备份
- [English]If的条件状语从句,表假设
- 窗口的创建过程
- 赛马问题(2014美团笔试题)
- 2015华为招聘机试样题(java实现)
- 对事件委托的理解
- ArithmeticException,IllegalArgumentException,NullPointerException,BufferUnderflowException