DevExpress控件 GridControl 单元格编辑 回车

时间:2021-12-30 14:14:50
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CYSoft.UI.Common;
using CYSoft.Common;
using CYSoft.TS.EntityLib;
using CYSoft.IC.Common.A0;
//Dev 引用
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository; namespace CYSoft.TS.UI.BookInfo
{
public partial class FrmCellEdit : TS.UI.Common.FrmEditBase
{
//接口
public IC.BookInfo bb = null;
public FrmCellEdit()
{
InitializeComponent(); bb = new IC.BookInfo(); InitCtl(); } /// <summary>
/// 初始化
/// </summary>
public void InitCtl() {
//克隆表结构 绑定到grid
DataTable dt = b.BookGetByCode("").Tables[0].Clone();
grid.DataSource = dt; //自定义方法 初始化 gv
CYSoft.UI.Common.Function.InitGrid(this.gv,
new string[] {"编码","名称" },
new string[] {"BookNo","BookName" },
new int[] {100,100 }); //编辑列 设置
RepositoryItemTextEdit rite = new RepositoryItemTextEdit();
//keydown事件
rite.KeyDown+=new KeyEventHandler(rite_KeyDown);
gv.Columns["BookNo"].ColumnEdit = rite; gv.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_CellValueChanged);
gv.InitNewRow += new DevExpress.XtraGrid.Views.Grid.InitNewRowEventHandler(gv_InitNewRow);
} /// <summary>
/// 单元格回车
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void rite_KeyDown(object sender, KeyEventArgs e) {
//用户点击 回车
if (e.KeyCode == Keys.Enter) {
TextEdit te = sender as TextEdit;
//值为空时返回
if (te == null || string.IsNullOrEmpty(te.Text.Trim())) {
return;
} //自定义方法,根据用户输入内容从数据库获取数据
DataSet ds = bb.BookGetByCode(te.Text.Trim());
//有数据记录
if (ds.Tables[0].Rows.Count > 0) {
//初始化DataRow 值为 第一行数据
DataRow dtBook = ds.Tables[0].Rows[0];
//初始化DataRow 值为
DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
foreach (DataColumn dc in dr.Table.Columns) {
dr[dc.ColumnName] = dtBook[dc.ColumnName].ToString();
}
//终止行编辑
dr.EndEdit();
//调用方法
SetCellEdit(gv.FocusedRowHandle);
//自定义类 对gv的一些设置
CYSoft.TS.UI.Common.TSFunc.SetBestFitColumns(gv);
} } } /// <summary>
/// 单元格处理
/// </summary>
/// <param name="row"></param>
private void SetCellEdit(int row)
{
Application.DoEvents();
try
{
gv.BeginInit();
grid.Focus();
gv.Focus();
gv.SelectCell(row, gv.Columns["BookNo"]);
gv.FocusedColumn = gv.Columns["BookNo"];
gv.ShowEditor();
}
finally
{
gv.EndInit();
}
} /// <summary>
/// 单元格 焦点离开 触发该事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void gv_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (e.Column.FieldName == "BookNo")
{
if (e.Value == null)
return;
}
} void gv_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
{
SetCellEdit(e.RowHandle);
} /// <summary>
/// 增加新行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNewLine_Click(object sender, EventArgs e)
{
DataTable dt = grid.DataSource as DataTable;
if (dt == null)
return; gv.AddNewRow();
}
}
}

DevExpress控件 GridControl  单元格编辑 回车

版权声明:本文为博主原创文章,未经博主允许不得转载。