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 PropertyDescrip torCollection. I get the following error:
"Additional information: Cannot create a child list for field Query."
Below my existing code:
public class FrmQueryManagem ent : 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 SqlCommandBuild er CmdBuilder;
private DataSet ds = new DataSet();
private System.Windows. Forms.GroupBox grpSelection;
private System.Windows. Forms.RadioButt on rbShowAll;
private System.Windows. Forms.RadioButt on rbStarted;
private System.Windows. Forms.RadioButt on rbEnded;
private System.Windows. Forms.RadioButt on rbNotAssigned;
//private System.Data.Sql Client.SqlConne ction SqlCn;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public FrmQueryManagem ent()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
DataGridCheckBo x();
GridBind();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.dgMain = new System.Windows. Forms.DataGrid( );
this.btnUpdate = new System.Windows. Forms.Button();
this.btnClose = new System.Windows. Forms.Button();
this.grpSelecti on = new System.Windows. Forms.GroupBox( );
this.rbNotAssig ned = new System.Windows. Forms.RadioButt on();
this.rbEnded = new System.Windows. Forms.RadioButt on();
this.rbStarted = new System.Windows. Forms.RadioButt on();
this.rbShowAll = new System.Windows. Forms.RadioButt on();
((System.Compon entModel.ISuppo rtInitialize)(t his.dgMain)).Be ginInit();
this.grpSelecti on.SuspendLayou t();
this.SuspendLay out();
//
// dgMain
//
this.dgMain.Cap tionText = "Query Management";
this.dgMain.Dat aMember = "";
this.dgMain.Hea derForeColor =
System.Drawing. SystemColors.Co ntrolText;
this.dgMain.Loc ation = new System.Drawing. Point(8, 80);
this.dgMain.Nam e = "dgMain";
this.dgMain.Siz e = new System.Drawing. Size(736, 216);
this.dgMain.Tab Index = 0;
this.dgMain.Mou seUp += new
System.Windows. Forms.MouseEven tHandler(this.d gMain_MouseUp);
this.dgMain.Nav igate += new
System.Windows. Forms.NavigateE ventHandler(thi s.dgMain_Naviga te);
//Initialise
private void GridBind()
{
String ConStr = MyGlobals.gsCon String;
SqlConnection myCon = new SqlConnection(C onStr);
try
{
myCon.Open();
}
catch (SqlException sqlex)
{
MessageBox.Show (sqlex.ToString ());
}
DataGridTableSt yle ts1 = new DataGridTableSt yle();
ts1.MappingName = "Query";
//Add the checkbox code here
DataGridColumnS tyle boolCol = new DataGridBoolCol umn();
boolCol.Mapping Name = "SelectStarted" ;
boolCol.HeaderT ext = "Start";
boolCol.Width = 30;
((DataGridBoolC olumn)boolCol). AllowNull = false;
ts1.GridColumnS tyles.Add(boolC ol);
DataGridColumnS tyle TextColStartDat e = new DataGridTextBox Column();
TextColStartDat e.MappingName = "StartDate" ; //from dataset table
TextColStartDat e.HeaderText = "Start Date";
TextColStartDat e.Width = 70;
TextColStartDat e.ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olStartDate);
DataGridColumnS tyle TextColStartUse r = new DataGridTextBox Column();
TextColStartUse r.MappingName = "StartUser" ; //from dataset table
TextColStartUse r.HeaderText = "Start User";
TextColStartUse r.Width = 60;
TextColStartUse r.ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olStartUser);
DataGridColumnS tyle boolColEnd = new DataGridBoolCol umn();
boolColEnd.Mapp ingName = "SelectEnde d";
boolColEnd.Head erText = "End";
boolColEnd.Widt h = 30;
((DataGridBoolC olumn)boolColEn d).AllowNull = false;
ts1.GridColumnS tyles.Add(boolC olEnd);
DataGridColumnS tyle TextColEndDate = new DataGridTextBox Column();
TextColEndDate. MappingName = "EndDate"; //from dataset table
TextColEndDate. HeaderText = "End Date";
TextColEndDate. Width = 70;
TextColEndDate. ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olEndDate);
DataGridColumnS tyle TextColEndUser = new DataGridTextBox Column();
TextColEndUser. MappingName = "EndUser"; //from dataset table
TextColEndUser. HeaderText = "End User";
TextColEndUser. Width = 60;
TextColEndUser. ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olEndUser);
DataGridColumnS tyle TextColFUDate = new DataGridTextBox Column();
TextColFUDate.M appingName = "Follow_Up_Date "; //from dataset table
TextColFUDate.H eaderText = "FU Date";
TextColFUDate.W idth = 70;
TextColFUDate.R eadOnly = true;
ts1.GridColumnS tyles.Add(TextC olFUDate);
PropertyDescrip torCollection pdc = this.BindingCon text[ds,
"Query"].GetItemPropert ies();
DataGridDigitsT extBoxColumn TextColResult = new
DataGridDigitsT extBoxColumn(pd c["Result_Comment "], "s", true);
//DataGridColumnS tyle TextColResult = new DataGridTextBox Column();
TextColResult.M appingName = "Result_Comment "; //from dataset table
TextColResult.H eaderText = "Result";
TextColResult.W idth = 50;
ts1.GridColumnS tyles.Add(TextC olResult);
DataGridColumnS tyle TextColFUStatus = new DataGridTextBox Column();
TextColFUStatus .MappingName = "Follow_up" ; //from dataset table
TextColFUStatus .HeaderText = "FU Status";
TextColFUStatus .Width = 70;
TextColFUStatus .ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olFUStatus);
DataGridColumnS tyle TextColContract Status = new
DataGridTextBox Column();
TextColContract Status.MappingN ame = "Contract_Statu s"; //from
dataset table
TextColContract Status.HeaderTe xt = "Contract Status";
TextColContract Status.Width = 70;
TextColContract Status.ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olContractStatu s);
DataGridColumnS tyle TextColEntitySt atus = new
DataGridTextBox Column();
TextColEntitySt atus.MappingNam e = "Entity_Status" ; //from dataset
table
TextColEntitySt atus.HeaderText = "Entity Status";
TextColEntitySt atus.Width = 70;
TextColEntitySt atus.ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olEntityStatus) ;
DataGridColumnS tyle TextColIDNo = new DataGridTextBox Column();
TextColIDNo.Map pingName = "Entity_Id_Numb er"; //from dataset table
TextColIDNo.Hea derText = "ID Number";
TextColIDNo.Wid th = 70;
TextColIDNo.Rea dOnly = true;
ts1.GridColumnS tyles.Add(TextC olIDNo);
DataGridColumnS tyle TextColContract No = new
DataGridTextBox Column();
TextColContract No.MappingName = "Contract_N o"; //from dataset table
TextColContract No.HeaderText = "Contract No";
TextColContract No.Width = 70;
TextColContract No.ReadOnly = true;
ts1.GridColumnS tyles.Add(TextC olContractNo);
DataGridColumnS tyle TextCol = new DataGridTextBox Column();
TextCol.Mapping Name = "Main_Name" ; //from dataset table
TextCol.HeaderT ext = "Main Name";
TextCol.Width = 100;
TextCol.ReadOnl y = true;
ts1.GridColumnS tyles.Add(TextC ol);
DataGridColumnS tyle TextCol2 = new DataGridTextBox Column();
TextCol2.Mappin gName = "First_Name "; //from dataset table
TextCol2.Header Text = "First Name";
TextCol2.Width = 100;
TextCol2.ReadOn ly = true;
ts1.GridColumnS tyles.Add(TextC ol2);
DataGridColumnS tyle csSeqNoInt = new DataGridTextBox Column();
csSeqNoInt.Mapp ingName = "SeqNo"; //from dataset table
csSeqNoInt.Head erText = "Seq No";
csSeqNoInt.Widt h = 20;
csSeqNoInt.Read Only = true;
ts1.GridColumnS tyles.Add(csSeq NoInt);
//Build the SQL statement for this user
string strWhere = " where Follow_Up in (";
SqlCommand cmdQry = new SqlCommand("sel ect * from
ctblrph_mo_quer y_management_se curity where UserName='" +
MyGlobals.gsUse rName + "'", myCon);
SqlDataReader dr;
dr = cmdQry.ExecuteR eader();
while (dr.Read())
{
strWhere = strWhere + "'" + dr.GetString(1) + "',";
}
strWhere = strWhere.Remove ((strWhere.Leng th-1),1);
strWhere = strWhere.Insert (strWhere.Lengt h,")");
dr.Close();
myCon.Close();
//MessageBox.Show (strWhere);
///instantiate SqlDataAdapter to create DataSet
//MessageBox.Show ("select * from ctblrph_mo_quer y_management " +
strWhere + " and (UserBeing='" + MyGlobals.gsUse rName + "' or
UserBeing = ''");
da = new SqlDataAdapter( "select * from ctblrph_mo_quer y_management
" + strWhere + " and (StartUser='" + MyGlobals.gsUse rName + "' or
StartUser is null)",myCon);
/*seqno, main_name,first _name, contract_no*/
/// the following is for creating automatic command builder
CmdBuilder = new SqlCommandBuild er(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.SetDataB inding(ds,"Quer y");
dgMain.TableSty les.Add(ts1);
}
private void dgMain_Navigate (object sender,
System.Windows. Forms.NavigateE ventArgs ne)
{
}
private void dgMain_MouseUp( object sender,
System.Windows. Forms.MouseEven tArgs e)
{
// Use the DataGrid control's HitTest method with the x and y
properties.
if(dgMain.HitTe st(e.X,e.Y).Col umn == 0)
{
dgMain[dgMain.CurrentC ell.RowNumber, 2] = MyGlobals.gsUse rName;
dgMain[dgMain.CurrentC ell.RowNumber, 1] = DateTime.Today;
}
if(dgMain.HitTe st(e.X,e.Y).Col umn == 3)
{
dgMain[dgMain.CurrentC ell.RowNumber, 5] = MyGlobals.gsUse rName;
dgMain[dgMain.CurrentC ell.RowNumber, 4] = DateTime.Today;
}
if(dgMain.HitTe st(e.X,e.Y).Col umn == 7) //Result
{
/*
if(dgMain[dgMain.CurrentC ell.RowNumber, 7] == "Y" ||
dgMain[dgMain.CurrentC ell.RowNumber, 7] == "N")
{
}
else
{
MessageBox.Show ("Invalid Result. Can only be Y or N!");
dgMain[dgMain.CurrentC ell.RowNumber, 7] = "";
}
*/
}
}
}
public class DataGridDigitsT extBoxColumn : DataGridTextBox Column
{
public DataGridDigitsT extBoxColumn(Sy stem.ComponentM odel.PropertyDe scriptor
pd, string format, bool b)
: base(pd, format, b)
{
this.TextBox.Ke yPress += new
System.Windows. Forms.KeyPressE ventHandler(Han dleKeyPress);
}
private void HandleKeyPress( object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
//ignore if not digit or control key
if(!char.IsDigi t(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.Se lectionLength == 0)
e.Handled = true;
}
protected override void Dispose(bool disposing)
{
if(disposing)
this.TextBox.Ke yPress -= new
System.Windows. Forms.KeyPressE ventHandler(Han dleKeyPress);
base.Dispose(di sposing);
}
}
}