winform里面,我对datagrid的textbox失去焦点的时候加验证,
如果textbox里面的值不合法,我想把焦点重新置回该textbox,
我加了focus(),但是没用,不知道有什么方法可以实现?请各位老大指教!
解决方案 »
- 如何实现两台计算之间简单通信
- windows2003下发布iis遇到人品级问题 !!!!!!!!!!!!!!!!
- combobox控件的存储使用~~
- 选中,怎么没弹出对话框呢
- windows应用程序如何模拟提交HTML表单给一个外部站点的asp页面?
- 如何在程序中选择打印纸类型?
- 在Internet 中能否asp.net使用windows验证及impersonate ?是否这样安全性更好?对于连接sql Server时使用windows验证也安全性更好?有什
- 如何将字符串"1/3"转化成float类型的值?
- 关于window service的问题。
- 怎样用adapter填充/更新DataSet里面有关联的表?
- 求助:datagrid的列的visible属性能不能根据不同的情况来定义true或false呢? 急~~谢谢
- C# label中显示不出从.txt文件中读出的中文字符,请指教!!!
{
try
{
//列名,列题头,列宽
int iCount=sAryColName.Length;
//
DataGridTableStyle tblStyle=new DataGridTableStyle();
tblStyle.MappingName= ATableName;
DataGridTextBoxColumn[] fldStyleAry=new DataGridTextBoxColumn[iCount];
for(int i=0;i<fldStyleAry.Length;i++)
{
fldStyleAry[i]=new DataGridTextBoxColumn();
}
tblStyle.GridColumnStyles.AddRange(fldStyleAry);AGrid.TableStyles.Clear();
AGrid.TableStyles.Add(tblStyle);
tblStyle.DataGrid= AGrid;for(int i=0;i<sAryColName.Length;i++)
{
DataGridTextBoxColumn NumberColumn = fldStyleAry[i];
NumberColumn.MappingName = sAryColName[i];
NumberColumn.HeaderText = sAryColText[i];
NumberColumn.Width = sAryColWidth[i];
NumberColumn.TextBox.Leave += new EventHandler(textLeave);//定义焦点离开的验证
}
}
catch
{
throw;
}
}
private void textLeave(object sender, System.EventArgs e)
{
Control innerBox = sender as Control;
if (innerBox.Text <> "abc")
{
innerBox.Focus();
}}///在你的程序中调用SetupGrid//数据库中的字段
stirng []m_sAryColName=new String[]
{
"f1", "f2", "f3", "f4", "f5"
};//对应到Grid上的中文名称
string[] m_sAryColText=new String[]
{
"中文1","中文2","中文3", "中文4", "中文5"
};//显示的宽度
int[] m_sAryColWidth=new int[]
{
0,100,100, 100,200
};//重新定制datagrid1的列名
SetupGrid(datagrid1, m_sAryColName, m_sAryColText, m_sAryColWidth, "Table");datagrid1.Datasource = "数据源";
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);
}
}}
折中的办法是参照楼上的方法定义全局变量
int oldrow,oldcol;
然后
在SetupGrid方法中添加
NumberColumn.TextBox.Enter += new EventHandler(textEnter);
NumberColumn.TextBox.LostFocus += new EventHandler(textLost);
去掉
NumberColumn.TextBox.Leave += new EventHandler(textLeave);//新增两个方法
private void textEnter(object sender, System.EventArgs e)
{
oldrow = dataGrid1.CurrentCell.RowNumber;
oldcol = dataGrid1.CurrentCell.ColumnNumber;
}private void textLost(object sender, System.EventArgs e)
{
TextBox innerBox = sender as TextBox;
if (innerBox.Text != "abc")
{
dataGrid1.CurrentCell = new DataGridCell(oldrow,oldcol);
innerBox.Focus();
}
}但不能使用鼠标点击, 点击以后有时候还是会跳出,所以这种方法也不可行
楼上的方法我没试,楼主可以试试看