Is it possible to check for duplicate Name before pushing the Save button

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
 
C

Cowboy \(Gregory A. Beamer\)

I assume this is a web application, as you can easily check with a windows
application.

Cell validating only works if you are taking a trip to the server. If you
are allowing a person to enter data into the grid from the client side, and
then submit, you will have to emit the JavaScript necessary to check and
clear the row if it fails validation.

If you are not JavaScript proficient, and this will work for you, you can
use a formview with the DataGrid to enter information. you then have a
submit mechanism before committing the line. if you don't want a full
postback, you can use AJAX, but this will allow you to catch duplicate
values, if it will work for you.

--
Gregory A. Beamer
MCP: +I, SE, SD, DBA

*********************************************
| Think outside the box!
|
*********************************************
 
C

Cor Ligthert[MVP]

Gregory,

DataGridView?

As was written DataGrid then it could have been confusing but the
DataGridView only exist in Forms (later then version 1.1)

I make 10 times a day this kind of mistakes.

:)

Cor
 
T

Tony Johansson

Hello!

I have set the unique property on the Name column to true which
mean that this is now unique and it works.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top