T
Tony Johansson
Hello!
I have a DataGridView with five columns. The first is called Name and must
be unique withing the DataGridView.
Now I check if the Name is unique when the Save button is pushed.
I just wonder if it's possible to make this check during entering data into
the DataGridView.
I have tried to fix this but I have run into problem.
The problem is this.
When the application starts and there are records in the textfile that is to
be loaded into the DataGridView this
dataGridViewWareHouse_CellValidating event handler is called.
The value that is to be checked is in the passed parameter e. I use
e.FormattedValue.ToString(); to make it a string
So if I check if this e.FormattedValue.ToString(); is within
the datatable the anser is yes because it's in
e.FormattedValue.ToString(); and in the Datatable.
So in some way I hope there is a chance to check if the input value already
exit in the DataTable.
Here is the small code that I have tried to
private void dataGridViewWareHouse_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
int myInteger;
dataGridViewWareHouse.Rows[e.RowIndex].ErrorText = "";
if ((dataGridViewWareHouse.Columns[e.ColumnIndex].DataPropertyName
== "Price") ||
(dataGridViewWareHouse.Columns[e.ColumnIndex].DataPropertyName
== "Count"))
{
if (!int.TryParse(e.FormattedValue.ToString(), out myInteger) ||
myInteger < 0)
{
dataGridViewWareHouse.Rows[e.RowIndex].ErrorText = "Invalid
data. Value must be a non-negative number";
e.Cancel = true;
}
}
else if
(dataGridViewWareHouse.Columns[e.ColumnIndex].DataPropertyName == "Name")
{
string input = e.FormattedValue.ToString();
string foo;
foreach (DataRow row in _dataTable.Rows)
{
foo = row["Name"].ToString();
}
}
}
//Tony
I have a DataGridView with five columns. The first is called Name and must
be unique withing the DataGridView.
Now I check if the Name is unique when the Save button is pushed.
I just wonder if it's possible to make this check during entering data into
the DataGridView.
I have tried to fix this but I have run into problem.
The problem is this.
When the application starts and there are records in the textfile that is to
be loaded into the DataGridView this
dataGridViewWareHouse_CellValidating event handler is called.
The value that is to be checked is in the passed parameter e. I use
e.FormattedValue.ToString(); to make it a string
So if I check if this e.FormattedValue.ToString(); is within
the datatable the anser is yes because it's in
e.FormattedValue.ToString(); and in the Datatable.
So in some way I hope there is a chance to check if the input value already
exit in the DataTable.
Here is the small code that I have tried to
private void dataGridViewWareHouse_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
int myInteger;
dataGridViewWareHouse.Rows[e.RowIndex].ErrorText = "";
if ((dataGridViewWareHouse.Columns[e.ColumnIndex].DataPropertyName
== "Price") ||
(dataGridViewWareHouse.Columns[e.ColumnIndex].DataPropertyName
== "Count"))
{
if (!int.TryParse(e.FormattedValue.ToString(), out myInteger) ||
myInteger < 0)
{
dataGridViewWareHouse.Rows[e.RowIndex].ErrorText = "Invalid
data. Value must be a non-negative number";
e.Cancel = true;
}
}
else if
(dataGridViewWareHouse.Columns[e.ColumnIndex].DataPropertyName == "Name")
{
string input = e.FormattedValue.ToString();
string foo;
foreach (DataRow row in _dataTable.Rows)
{
foo = row["Name"].ToString();
}
}
}
//Tony