Hi
how do I validate a text value in a datagrid e.g. the user is only
allowed to type in a Y or a N. I specifically have a problem with
using the PropertyDescriptorCollection. I get the following error:
"Additional information: Cannot create a child list for field Query."
Below my existing code:
public class FrmQueryManagement : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dgMain;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Button btnClose;
// Declaring...
private SqlDataAdapter da;
private SqlCommandBuilder CmdBuilder;
private DataSet ds = new DataSet();
private System.Windows.Forms.GroupBox grpSelection;
private System.Windows.Forms.RadioButton rbShowAll;
private System.Windows.Forms.RadioButton rbStarted;
private System.Windows.Forms.RadioButton rbEnded;
private System.Windows.Forms.RadioButton rbNotAssigned;
//private System.Data.SqlClient.SqlConnection SqlCn;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FrmQueryManagement()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
DataGridCheckBox();
GridBind();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <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.dgMain = new System.Windows.Forms.DataGrid();
this.btnUpdate = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.grpSelection = new System.Windows.Forms.GroupBox();
this.rbNotAssigned = new System.Windows.Forms.RadioButton();
this.rbEnded = new System.Windows.Forms.RadioButton();
this.rbStarted = new System.Windows.Forms.RadioButton();
this.rbShowAll = new System.Windows.Forms.RadioButton();
((System.ComponentModel.ISupportInitialize)(this.d gMain)).BeginInit();
this.grpSelection.SuspendLayout();
this.SuspendLayout();
//
// dgMain
//
this.dgMain.CaptionText = "Query Management";
this.dgMain.DataMember = "";
this.dgMain.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dgMain.Location = new System.Drawing.Point(8, 80);
this.dgMain.Name = "dgMain";
this.dgMain.Size = new System.Drawing.Size(736, 216);
this.dgMain.TabIndex = 0;
this.dgMain.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.dgMain _MouseUp);
this.dgMain.Navigate += new
System.Windows.Forms.NavigateEventHandler(this.dgM ain_Navigate);
//Initialise
private void GridBind()
{
String ConStr = MyGlobals.gsConString;
SqlConnection myCon = new SqlConnection(ConStr);
try
{
myCon.Open();
}
catch (SqlException sqlex)
{
MessageBox.Show(sqlex.ToString());
}
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Query";
//Add the checkbox code here
DataGridColumnStyle boolCol = new DataGridBoolColumn();
boolCol.MappingName = "SelectStarted";
boolCol.HeaderText = "Start";
boolCol.Width = 30;
((DataGridBoolColumn)boolCol).AllowNull = false;
ts1.GridColumnStyles.Add(boolCol);
DataGridColumnStyle TextColStartDate = new DataGridTextBoxColumn();
TextColStartDate.MappingName = "StartDate"; //from dataset table
TextColStartDate.HeaderText = "Start Date";
TextColStartDate.Width = 70;
TextColStartDate.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColStartDate);
DataGridColumnStyle TextColStartUser = new DataGridTextBoxColumn();
TextColStartUser.MappingName = "StartUser"; //from dataset table
TextColStartUser.HeaderText = "Start User";
TextColStartUser.Width = 60;
TextColStartUser.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColStartUser);
DataGridColumnStyle boolColEnd = new DataGridBoolColumn();
boolColEnd.MappingName = "SelectEnded";
boolColEnd.HeaderText = "End";
boolColEnd.Width = 30;
((DataGridBoolColumn)boolColEnd).AllowNull = false;
ts1.GridColumnStyles.Add(boolColEnd);
DataGridColumnStyle TextColEndDate = new DataGridTextBoxColumn();
TextColEndDate.MappingName = "EndDate"; //from dataset table
TextColEndDate.HeaderText = "End Date";
TextColEndDate.Width = 70;
TextColEndDate.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColEndDate);
DataGridColumnStyle TextColEndUser = new DataGridTextBoxColumn();
TextColEndUser.MappingName = "EndUser"; //from dataset table
TextColEndUser.HeaderText = "End User";
TextColEndUser.Width = 60;
TextColEndUser.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColEndUser);
DataGridColumnStyle TextColFUDate = new DataGridTextBoxColumn();
TextColFUDate.MappingName = "Follow_Up_Date"; //from dataset table
TextColFUDate.HeaderText = "FU Date";
TextColFUDate.Width = 70;
TextColFUDate.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColFUDate);
PropertyDescriptorCollection pdc = this.BindingContext[ds,
"Query"].GetItemProperties();
DataGridDigitsTextBoxColumn TextColResult = new
DataGridDigitsTextBoxColumn(pdc["Result_Comment"], "s", true);
//DataGridColumnStyle TextColResult = new DataGridTextBoxColumn();
TextColResult.MappingName = "Result_Comment"; //from dataset table
TextColResult.HeaderText = "Result";
TextColResult.Width = 50;
ts1.GridColumnStyles.Add(TextColResult);
DataGridColumnStyle TextColFUStatus = new DataGridTextBoxColumn();
TextColFUStatus.MappingName = "Follow_up"; //from dataset table
TextColFUStatus.HeaderText = "FU Status";
TextColFUStatus.Width = 70;
TextColFUStatus.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColFUStatus);
DataGridColumnStyle TextColContractStatus = new
DataGridTextBoxColumn();
TextColContractStatus.MappingName = "Contract_Status"; //from
dataset table
TextColContractStatus.HeaderText = "Contract Status";
TextColContractStatus.Width = 70;
TextColContractStatus.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColContractStatus);
DataGridColumnStyle TextColEntityStatus = new
DataGridTextBoxColumn();
TextColEntityStatus.MappingName = "Entity_Status"; //from dataset
table
TextColEntityStatus.HeaderText = "Entity Status";
TextColEntityStatus.Width = 70;
TextColEntityStatus.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColEntityStatus);
DataGridColumnStyle TextColIDNo = new DataGridTextBoxColumn();
TextColIDNo.MappingName = "Entity_Id_Number"; //from dataset table
TextColIDNo.HeaderText = "ID Number";
TextColIDNo.Width = 70;
TextColIDNo.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColIDNo);
DataGridColumnStyle TextColContractNo = new
DataGridTextBoxColumn();
TextColContractNo.MappingName = "Contract_No"; //from dataset table
TextColContractNo.HeaderText = "Contract No";
TextColContractNo.Width = 70;
TextColContractNo.ReadOnly = true;
ts1.GridColumnStyles.Add(TextColContractNo);
DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "Main_Name"; //from dataset table
TextCol.HeaderText = "Main Name";
TextCol.Width = 100;
TextCol.ReadOnly = true;
ts1.GridColumnStyles.Add(TextCol);
DataGridColumnStyle TextCol2 = new DataGridTextBoxColumn();
TextCol2.MappingName = "First_Name"; //from dataset table
TextCol2.HeaderText = "First Name";
TextCol2.Width = 100;
TextCol2.ReadOnly = true;
ts1.GridColumnStyles.Add(TextCol2);
DataGridColumnStyle csSeqNoInt = new DataGridTextBoxColumn();
csSeqNoInt.MappingName = "SeqNo"; //from dataset table
csSeqNoInt.HeaderText = "Seq No";
csSeqNoInt.Width = 20;
csSeqNoInt.ReadOnly = true;
ts1.GridColumnStyles.Add(csSeqNoInt);
//Build the SQL statement for this user
string strWhere = " where Follow_Up in (";
SqlCommand cmdQry = new SqlCommand("select * from
ctblrph_mo_query_management_security where UserName='" +
MyGlobals.gsUserName + "'", myCon);
SqlDataReader dr;
dr = cmdQry.ExecuteReader();
while (dr.Read())
{
strWhere = strWhere + "'" + dr.GetString(1) + "',";
}
strWhere = strWhere.Remove((strWhere.Length-1),1);
strWhere = strWhere.Insert(strWhere.Length,")");
dr.Close();
myCon.Close();
//MessageBox.Show(strWhere);
///instantiate SqlDataAdapter to create DataSet
//MessageBox.Show ("select * from ctblrph_mo_query_management " +
strWhere + " and (UserBeing='" + MyGlobals.gsUserName + "' or
UserBeing = ''");
da = new SqlDataAdapter("select * from ctblrph_mo_query_management
" + strWhere + " and (StartUser='" + MyGlobals.gsUserName + "' or
StartUser is null)",myCon);
/*seqno, main_name,first_name, contract_no*/
/// the following is for creating automatic command builder
CmdBuilder = new SqlCommandBuilder(da);
///fill the dataset
try
{
da.Fill(ds, "Query");
}
catch (SqlException sqlex)
{
MessageBox.Show(sqlex.ToString());
}
///set the dataset as a datasource for windows datagrid
dgMain.SetDataBinding(ds,"Query");
dgMain.TableStyles.Add(ts1);
}
private void dgMain_Navigate(object sender,
System.Windows.Forms.NavigateEventArgs ne)
{
}
private void dgMain_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Use the DataGrid control's HitTest method with the x and y
properties.
if(dgMain.HitTest(e.X,e.Y).Column == 0)
{
dgMain[dgMain.CurrentCell.RowNumber, 2] = MyGlobals.gsUserName;
dgMain[dgMain.CurrentCell.RowNumber, 1] = DateTime.Today;
}
if(dgMain.HitTest(e.X,e.Y).Column == 3)
{
dgMain[dgMain.CurrentCell.RowNumber, 5] = MyGlobals.gsUserName;
dgMain[dgMain.CurrentCell.RowNumber, 4] = DateTime.Today;
}
if(dgMain.HitTest(e.X,e.Y).Column == 7) //Result
{
/*
if(dgMain[dgMain.CurrentCell.RowNumber, 7] == "Y" ||
dgMain[dgMain.CurrentCell.RowNumber, 7] == "N")
{
}
else
{
MessageBox.Show("Invalid Result. Can only be Y or N!");
dgMain[dgMain.CurrentCell.RowNumber, 7] = "";
}
*/
}
}
}
public class DataGridDigitsTextBoxColumn : DataGridTextBoxColumn
{
public DataGridDigitsTextBoxColumn(System.ComponentModel. PropertyDescriptor
pd, string format, bool b)
: base(pd, format, b)
{
this.TextBox.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(HandleKe yPress);
}
private void HandleKeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
//ignore if not digit or control key
if(!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
e.Handled = true;
//ignore if more than 3 digits
if(this.TextBox.Text.Length >= 3 && !char.IsControl(e.KeyChar) &&
this.TextBox.SelectionLength == 0)
e.Handled = true;
}
protected override void Dispose(bool disposing)
{
if(disposing)
this.TextBox.KeyPress -= new
System.Windows.Forms.KeyPressEventHandler(HandleKe yPress);
base.Dispose(disposing);
}
}
}