473,289 Members | 1,929 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,289 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 3171

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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.