急救 如何在DataGridView控件中验证数据输入 数据输入错误时,弹出错误提示!~~~~~~~~~~~~~~~~~在线等待!!!
源码
源码
解决方案 »
- public void和public static有什么区别
- 麻烦解释下这几句
- 请问C#中,比较byte[] array1和byte[] array2相等的函数有没有,是什么?谢谢
- 询问“橡皮擦”功能
- 请问各位,固定richtextbox 的宽度,然后让它的高度根据输入的内容自动适应,有什么办法?
- 关于ImageList控件的使用问题(急!!在线等)
- 拓扑结构图生成,恳求帮助!
- 一个很简单的关于字符串截取的问题请高手给点建议
- 怎么使用资源管理器的资源
- 请教高手,这个问题怎么处理?
- .net winform里声明的public属性外部为何无法访问?
- 急急急求解决方法,关于GenerateApplicationManifest
{
if (this.dataGridView1.Rows[0].Cells[0].Value.ToString() == "")
{
MessageBox.Show("第一行第一列的值不能为空!");
}
}
不知这样能不能满足你的需求!
比较简单
lz自己试一下就知道了
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
{
private DataGridView dataGridView1 = new DataGridView();
private BindingSource bindingSource1 = new BindingSource(); public Form1()
{
// Initialize the form.
this.dataGridView1.Dock = DockStyle.Fill;
this.Controls.Add(dataGridView1);
this.Load += new EventHandler(Form1_Load);
this.Text = "DataGridView validation demo (disallows empty CompanyName)";
} private void Form1_Load(System.Object sender, System.EventArgs e)
{
// Attach DataGridView events to the corresponding event handlers.
this.dataGridView1.CellValidating += new
DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
this.dataGridView1.CellEndEdit += new
DataGridViewCellEventHandler(dataGridView1_CellEndEdit); // Initialize the BindingSource and bind the DataGridView to it.
bindingSource1.DataSource = GetData("select * from Customers");
this.dataGridView1.DataSource = bindingSource1;
this.dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
} private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings.
if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
{
if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}
}
} void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Clear the row error in case the user presses ESC.
dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
} private static DataTable GetData(string selectCommand)
{
string connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost;Packet Size=4096"; // Connect to the database and fill a data table.
SqlDataAdapter adapter =
new SqlDataAdapter(selectCommand, connectionString);
DataTable data = new DataTable();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(data); return data;
} [STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}}
private void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("CustomerID value must be unique.");
}
}
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings.
if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
{
try
{
int val = Int32.Parse(e.FormattedValue.ToString());
if(val < 10 || val > 100)
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must be >10 and <100";
e.Cancel = true;
}
}
catch(Exception ex){
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must be number";
e.Cancel = true;
}
}
}