473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Validating datagrid values

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);

}
}
}
Nov 15 '05 #1
0 3175

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Omavlana | last post by:
Pleas advise me how to do validations in side datagrid before updating the details into database. I am using VB.NET and updating the datagrid using sqlcommandbuilder. I want to validate the...
0
by: Morné | last post by:
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...
0
by: enahar | last post by:
I want to validate a cell wheather a value is greater than 0 or not.If value is greater than 0 then I want to show messagebox and focus back the same cell else move to the other cell. I am...
0
by: SMai24 | last post by:
Anyone has the same experience? I am trying to validate one of the textboxes inside a datagrid, but everytime when i click my mouse on another cell, the validating fires twice. Help plz....
0
by: Ante Perkovic | last post by:
Hi, After long search of the internet, I couldn't find any solution on validating a datagrid. I have textboxes and validators (required field) in <editItemTemplate> and labels in <ItemTemplate>....
3
by: Devlei | last post by:
I have a column in a datagrid that I want to limit the values to integers from 1 to 5. What is the best way to do this? The datagrid is bound to a typed dataset that is populated from an XML...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
2
by: Jason Huang | last post by:
Hi, In my .Net C# Windows form Form1, there's a DataGrid DataGrid1 on Form1. How do I validate if the values in DataGrid1 is modified or not? Thanks for help. Jason
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.