c#(winform)单元格焦点的问题

wyhflying 2005-11-21 05:56:35
请教个个问题啊(winform):就是在dataGrid中,在cell(0,0)输入一个字符串后,按下回车键时判断cell(0,0) 的输入是否合法,如果合法则跳转到cell(0,1) 不合法则焦点继续停留在cell(0,0)并清空此单元格内的内容,
我在cell(0,0) 中输入后,按回车健取不到输入的信息(dataGrid[0,0].ToString())请问是怎么回事 ?
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxuu503 2005-11-22
  • 打赏
  • 举报
回复
回车键,被datagrid吃掉了吧
北京的雾霾天 2005-11-22
  • 打赏
  • 举报
回复
从datagrid继承一个新的类,然后添加如下的重写:

protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
{
Console.WriteLine(this.CurrentCell.ToString());
base.ProcessDialogKey(keyData);
}
wyhflying 2005-11-22
  • 打赏
  • 举报
回复
花生:该怎么重载呢?给个思路好吗?
runrunrun:没有你说的这个事件啊
q_po_o 2005-11-22
  • 打赏
  • 举报
回复
转贴
符合你的需求

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridValidation
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet myDataSet;

int newCurrentRow;
int newCurrentCol;
int oldCurrentRow;
int oldCurrentCol;
private bool okToValidate;


/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

newCurrentRow = -1;
newCurrentCol = -1;
okToValidate = true;


MakeDataSet();
InitDataGrid();


//
// TODO: Add any constructor code after InitializeComponent call
//
}

public void InitDataGrid()
{
dataGrid1.SetDataBinding(myDataSet, "Codes");
AddCustomDataTableStyle();
}

private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Codes";
// Set other properties.
ts1.AlternatingBackColor = Color.LightBlue;
ts1.RowHeaderWidth = 20;

// col 0
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "BlueCode";
TextCol.HeaderText = "< 400";
TextCol.Width = 80;
TextCol.NullText = "0";
ts1.GridColumnStyles.Add(TextCol);

// col 1
TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "GreenCode";
TextCol.HeaderText = "< 600";
TextCol.Width = 80;
TextCol.NullText = "0";
ts1.GridColumnStyles.Add(TextCol);

// col 2
TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "RedCode";
TextCol.HeaderText = "Any";
TextCol.Width = 80;
TextCol.NullText = "0";

ts1.GridColumnStyles.Add(TextCol);

dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(ts1);

}

private void MakeDataSet()
{
// Create a DataSet.
myDataSet = new DataSet("CodesDataSet");

// Create code strings table
DataTable tableStrings = new DataTable("Codes");

// Create a columns, and add them to the first table.
DataColumn colCodeStrings = new DataColumn("BlueCode");
tableStrings.Columns.Add(colCodeStrings);

colCodeStrings = new DataColumn("GreenCode");
tableStrings.Columns.Add(colCodeStrings);

colCodeStrings = new DataColumn("RedCode");
tableStrings.Columns.Add(colCodeStrings);

// Add the tables to the DataSet.
myDataSet.Tables.Add(tableStrings);

/* Populates the table*/
DataRow newRow1;
newRow1 = tableStrings.NewRow();
newRow1["BlueCode"] = "100";
newRow1["GreenCode"] = "100";
newRow1["RedCode"] = "100";
tableStrings.Rows.Add(newRow1);

newRow1 = tableStrings.NewRow();
newRow1["BlueCode"] = "200";
newRow1["GreenCode"] = "200";
newRow1["RedCode"] = "200";
tableStrings.Rows.Add(newRow1);

newRow1 = tableStrings.NewRow();
newRow1["BlueCode"] = "300";
newRow1["GreenCode"] = "300";
newRow1["RedCode"] = "300";
tableStrings.Rows.Add(newRow1);



}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(40, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(544, 240);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.Handle_CurrentCellChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(632, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

// validation test depending on row, col
// return true if valid value
public bool IsValidValue(int row, int col, string newText)
{
bool returnValue = true;
try
{
if(col == 0)
returnValue = (int.Parse(newText) < 400);
else if(col == 1)
returnValue = (int.Parse(newText) < 600);
}
catch(Exception ex)
{
//likely Parse throws an error for invalid chars
returnValue = false;
}

return returnValue;
}


private void Handle_CurrentCellChanged(object sender, System.EventArgs e)
{
newCurrentRow = dataGrid1.CurrentCell.RowNumber;
newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
{
MessageBox.Show("Entry Error");
okToValidate = false;
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
okToValidate = true;

}
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}

private void Form1_Load(object sender, System.EventArgs e)
{
//set to initial current cell
oldCurrentRow = 0;
oldCurrentCol = 0;;
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
}
}
}
runrunrun 2005-11-21
  • 打赏
  • 举报
回复
BeforeUpdate事件
自由程序员 2005-11-21
  • 打赏
  • 举报
回复
重载方法

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧