Hi,
> Hi Bart
> Thank you for your reply
[quoted text clipped - 3 lines]
> this error
> System.IndexOutOfRange.Exception: No value at index 3
I don't know a way to detect the last row, but i do think it's possible to
protect yourself against accessing a row that has disappeared :
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
int newCurrentRow = dataGrid1.CurrentCell.RowNumber;
int newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
int rowCount =
BindingContext[dataGrid1.DataSource,dataGrid1.DataMember].Count;
object val = (oldCurrentRow<rowCount)?dataGrid1[oldCurrentRow,
oldCurrentCol]:null;
if ( (val != null) && (val != DBNull.Value) &&
(oldCurrentRow < rowCount) &&
okToValidate &&
!IsValidValue(oldCurrentRow, oldCurrentCol, val.ToString()))
{
MessageBox.Show("Entry Error");
okToValidate = false;
CheckAgain = false;
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
okToValidate = true;
CheckAgain = true;
}
else
{
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
}//method
HTH,
Greetings
> Best Regards
> Stanislav.
[quoted text clipped - 47 lines]
>> HTH,
>> Greetings
Stanislav Nedelchev - 30 Nov 2005 11:28 GMT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi Bart Mermuys,<br>
Thank you .<br>
This is working.<br>
<br>
<br>
private void Handle_CurrentCellChanged(object sender, System.EventArgs
e)<br>
{<br>
try <br>
{<br>
int newCurrentRow = dataGridParts.CurrentCell.RowNumber;<br>
int newCurrentCol =
dataGridParts.CurrentCell.ColumnNumber;<br>
int rowCount =
BindingContext[dataGridParts.DataSource,dataGridParts.DataMember].Count;<br>
object val =
(oldCurrentRow<rowCount)?dataGridParts[oldCurrentRow,
oldCurrentCol]:null;<br>
sqlConnection1.Close();<br>
if ( (val != null) && (val != DBNull.Value) <br>
&& (oldCurrentRow < rowCount) <br>
&& okToValidate <br>
&& !IsValidValue(oldCurrentRow,
oldCurrentCol, val.ToString())<br>
)<br>
{<br>
MessageBox.Show("Entry Error");<br>
okToValidate = false;<br>
dataGridParts.CurrentCell = new
DataGridCell(oldCurrentRow, oldCurrentCol);<br>
okToValidate = true;<br>
<br>
}<br>
else<br>
{<br>
oldCurrentRow = newCurrentRow;<br>
oldCurrentCol = newCurrentCol;<br>
}<br>
}<br>
<br>
<br>
catch (Exception ex)<br>
{<br>
MessageBox.Show(ex.ToString());<br>
<br>
}<br>
<br>
}<br>
<br>
<br>
<br>
<blockquote cite="mid%23Kw8BnZ9FHA.808@TK2MSFTNGP09.phx.gbl" type="cite">
<pre wrap="">Hi,
</pre>
<blockquote type="cite">
<pre wrap="">Hi Bart
Thank you for your reply
I think the problem is when you click at end of the grid
new empty record is added and when you get back without making any
changes to new record datagrid is removing new record and then i get
this error
System.IndexOutOfRange.Exception: No value at index 3
</pre>
</blockquote>
<pre wrap=""><!---->
I don't know a way to detect the last row, but i do think it's possible to
protect yourself against accessing a row that has disappeared :
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
int newCurrentRow = dataGrid1.CurrentCell.RowNumber;
int newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
int rowCount =
BindingContext[dataGrid1.DataSource,dataGrid1.DataMember].Count;
object val = (oldCurrentRow<rowCount)?dataGrid1[oldCurrentRow,
oldCurrentCol]:null;
if ( (val != null) && (val != DBNull.Value) &&
(oldCurrentRow < rowCount) &&
okToValidate &&
!IsValidValue(oldCurrentRow, oldCurrentCol, val.ToString()))
{
MessageBox.Show("Entry Error");
okToValidate = false;
CheckAgain = false;
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
okToValidate = true;
CheckAgain = true;
}
else
{
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
}//method
HTH,
Greetings
</pre>
<blockquote type="cite">
<pre wrap="">Best Regards
Stanislav.
</pre>
<blockquote type="cite">
<pre wrap="">Hi,
</pre>
<blockquote type="cite">
<pre wrap="">Hi to all,
I have problem with detecting null values in datagrid cell
here is the example code i get i get runtime error when i'm trying to
convert datagrid cell to string if it have null value and i want to
make check if cell have null value i want to skip operation.
Here is the example code
</pre>
</blockquote>
<pre wrap="">Not sure if this will help but i rearranged your code a little, because
it
didn't seem to work correctly (though i did not get an Exception with
your
code), because the oldrow and oldcol weren't updated when the old cell
was
null and when validation failed they were updated when they shouldn't.
See
changes made:
private void Handle_CurrentCellChanged(object sender, System.EventArgs e)
{
newCurrentRow = dataGridParts.CurrentCell.RowNumber;
newCurrentCol = dataGridParts.CurrentCell.ColumnNumber;
object val = dataGridParts[oldCurrentRow, oldCurrentCol];
if ( ( val != DBNull.value ) && ( val != null ) &&
( okToValidate ) &&
( !IsValidValue(oldCurentRow, oldCurrentCol, val.ToString())
{
// validation fails
MessageBox.Show("Entry Error");
okToValidate = false;
CheckAgain = false;
dataGridParts.CurrentCell = new DataGridCell(oldCurrentRow,
oldCurrentCol);
okToValidate = true;
CheckAgain = true;
}
else
{
// validation succeeded
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
}
HTH,
Greetings
</pre>
</blockquote>
</blockquote>
<pre wrap=""><!---->
</pre>
</blockquote>
<br>
</body>
</html>
Stanislav Nedelchev - 30 Nov 2005 11:31 GMT
Hi Bart Mermuys,
Thank you .
This is working.
private void Handle_CurrentCellChanged(object sender, System.EventArgs e)
{
try
{
int newCurrentRow = dataGridParts.CurrentCell.RowNumber;
int newCurrentCol = dataGridParts.CurrentCell.ColumnNumber;
int rowCount =
BindingContext[dataGridParts.DataSource,dataGridParts.DataMember].Count;
object val =
(oldCurrentRow<rowCount)?dataGridParts[oldCurrentRow, oldCurrentCol]:null;
sqlConnection1.Close();
if ( (val != null) && (val != DBNull.Value)
&& (oldCurrentRow < rowCount)
&& okToValidate
&& !IsValidValue(oldCurrentRow, oldCurrentCol,
val.ToString())
)
{
MessageBox.Show("Entry Error");
okToValidate = false;
dataGridParts.CurrentCell = new
DataGridCell(oldCurrentRow, oldCurrentCol);
okToValidate = true;
}
else
{
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
> Hi,
>
[quoted text clipped - 100 lines]
>
>