Code Snippet
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private DataSet dsCategoryField;
public Form1()
{
this.cboCategoryField = new System.Windows.Forms.ComboBox();
this.dgvCategory = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dgvCategory)).BeginInit();
this.SuspendLayout();
//
// cboCategoryField
//
this.cboCategoryField.FormattingEnabled = true;
this.cboCategoryField.Location = new System.Drawing.Point(12, 12);
this.cboCategoryField.Name = "cboCategoryField";
this.cboCategoryField.Size = new System.Drawing.Size(121, 21);
this.cboCategoryField.TabIndex = 0;
//
// dgvCategory
//
this.dgvCategory.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvCategory.Location = new System.Drawing.Point(12, 39);
this.dgvCategory.Name = "dgvCategory";
this.dgvCategory.Size = new System.Drawing.Size(394, 264);
this.dgvCategory.TabIndex = 1;
this.dgvCategory.RowValidating += new System.Windows.Forms.DataGridViewCellCancelEventHandler(this.dgvCategory_RowValidating);
this.dgvCategory.CellParsing += new System.Windows.Forms.DataGridViewCellParsingEventHandler(this.dgvCategory_CellParsing);
this.dgvCategory.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dgvCategory_CellValidating);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(476, 393);
this.Controls.Add(this.dgvCategory);
this.Controls.Add(this.cboCategoryField);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dgvCategory)).EndInit();
this.ResumeLayout(false);
}
private void Form1_Load(object sender, EventArgs e)
{
dsCategoryField = new DataSet("CategoryDataSet");
DataTable dtCategoryField = new DataTable("CategoryField");
DataColumn dcCategoryFieldID = new DataColumn("ID", typeof(int));
DataColumn dcCategoryFieldName = new DataColumn("Name", typeof(string));
dtCategoryField.Columns.Add(dcCategoryFieldID);
dtCategoryField.Columns.Add(dcCategoryFieldName);
DataTable dtCategory = new DataTable("Category");
DataColumn dcCategoryID = new DataColumn("ID", typeof(int));
DataColumn dcCategoryFKID = new DataColumn("CategoryFieldID", typeof(int));
DataColumn dcCategoryCode = new DataColumn("Code", typeof(string));
DataColumn dcCategoryDescription = new DataColumn("Description", typeof(string));
dtCategory.Columns.Add(dcCategoryID);
dtCategory.Columns.Add(dcCategoryFKID);
dtCategory.Columns.Add(dcCategoryCode);
dtCategory.Columns.Add(dcCategoryDescription);
dsCategoryField.Tables.Add(dtCategoryField);
dsCategoryField.Tables.Add(dtCategory);
DataRelation myRelation = new DataRelation("Relation_00",
dsCategoryField.Tables["CategoryField"].Columns["ID"],
dsCategoryField.Tables["Category"].Columns["CategoryFieldID"]);
dsCategoryField.Relations.Add(myRelation);
AddCategoryField(1,"Category");
AddCategoryField(2,"SubCategory");
AddCategoryField(3,"ECategory");
AddCategoryField(4,"ESubCategory");
BindingSource bsCategoryField = new BindingSource(dsCategoryField,"CategoryField");
BindingSource bsCategory = new BindingSource(bsCategoryField,"Relation_00");
cboCategoryField.DataSource = bsCategoryField;
cboCategoryField.DisplayMember = "Name";
cboCategoryField.ValueMember = "ID";
dgvCategory.DataSource = bsCategory;
}
private void AddCategoryField(int id, string name){
DataRow row = dsCategoryField.Tables["CategoryField"].NewRow();
row["ID"] = id;
row["Name"] = name;
dsCategoryField.Tables["CategoryField"].Rows.Add(row);
}
private void dgvCategory_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
if (e.ColumnIndex == 2) {//Code Column
if (e.Value != null) {
e.Value = e.Value.ToString().PadRight(5);
e.ParsingApplied = true;
}
}
if (e.ColumnIndex == 3) {//Description Column
if (e.Value != null) {
e.Value = e.Value.ToString().Trim();
e.ParsingApplied = true;
}
}
}
private void dgvCategory_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dgvCategory.Rows[e.RowIndex].DataBoundItem != null) {
DataRow row = ((DataRowView)dgvCategory.Rows[e.RowIndex].DataBoundItem).Row;
string errorText = "";
if (e.ColumnIndex == 2) {
if (!ValidateCode(e.FormattedValue.ToString(),row)) {
errorText = "The Code '" + e.FormattedValue + "' has already been defined for the category '" +
cboCategoryField.Text + "'.";
}
}
if (e.ColumnIndex == 3) {
if (!ValidateDescription(e.FormattedValue.ToString())) {
errorText = "Description must not be empty";
}
}
dgvCategory.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = errorText;
}
}
//This is the section of code in question
private void dgvCategory_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.RowIndex < dgvCategory.Rows.Count &&
dgvCategory.Rows[e.RowIndex].DataBoundItem != null) {
DataGridViewRow dgvRow = dgvCategory.Rows[e.RowIndex];
DataRow dtRow =((DataRowView)dgvRow.DataBoundItem).Row;
StringBuilder errorText = new StringBuilder("", 40);
bool isPlural = false;
string codeValue = dgvRow.Cells[2].Value.ToString();
string descriptionValue = dgvRow.Cells[3].Value.ToString();
if (string.IsNullOrEmpty(codeValue) ||
!ValidateCode(codeValue,dtRow)) {
errorText.Append("Code");
}
if (!ValidateDescription(descriptionValue)) {
if (errorText.Length > 0) {
errorText.Append(" and ");
isPlural = true;
}
errorText.Append("Description");
}
if (errorText.Length > 0) {
if (isPlural) {
errorText.Append(" values are invalid");
}
else {
errorText.Append(" value is invalid");
}
e.Cancel = true;
}
dgvCategory.Rows[e.RowIndex].ErrorText = errorText.ToString();
}
}
private bool ValidateCode(string value, DataRow row)
{
DataRow[] rows = dsCategoryField.Tables["Category"].Select("CategoryFieldID = '" +
cboCategoryField.SelectedValue + "' AND Code = '" + value +"'");
if (rows.Length == 0 ||
(rows.Length == 1 &&
rows[0] == row)) {
return true;
}
else {
return false;
}
}
private bool ValidateDescription(string value)
{
return (string.Empty != value);
}
}
}