Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2005 >

dotnet windows forms databinding : DataGrid null values !


Stanislav Nedelchev
11/29/2005 11:10:24 AM
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

private void Handle_CurrentCellChanged(object sender, System.EventArgs e)
{


newCurrentRow = dataGridParts.CurrentCell.RowNumber;
newCurrentCol = dataGridParts.CurrentCell.ColumnNumber;

//Here is the check for null value
if ((dataGridParts[oldCurrentRow, oldCurrentCol]).ToString().Trim() !=
System.DBNull.Value.ToString())
{
string newText = dataGridParts[oldCurrentRow, oldCurrentCol].ToString();
sqlConnection1.Close();
if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
{
MessageBox.Show("Entry Error");
okToValidate = false;
CheckAgain = false;
dataGridParts.CurrentCell = new DataGridCell(oldCurrentRow,
oldCurrentCol);
okToValidate = true;
CheckAgain = true;

}

oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}


Bart Mermuys
11/29/2005 4:55:35 PM
Hi,

[quoted text, click to view]

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

Stanislav Nedelchev
11/30/2005 12:00:00 AM
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

Best Regards
Stanislav.

[quoted text, click to view]
Bart Mermuys
11/30/2005 11:33:18 AM
Hi,

[quoted text, click to view]

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

[quoted text, click to view]

Stanislav Nedelchev
11/30/2005 1:28:50 PM
<!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>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int newCurrentRow = dataGridParts.CurrentCell.RowNumber;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int newCurrentCol =
dataGridParts.CurrentCell.ColumnNumber;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int rowCount =
BindingContext[dataGridParts.DataSource,dataGridParts.DataMember].Count;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; object val =
(oldCurrentRow&lt;rowCount)?dataGridParts[oldCurrentRow,
oldCurrentCol]:null;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlConnection1.Close();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ( (val != null) &amp;&amp; (val != DBNull.Value) <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; (oldCurrentRow &lt; rowCount) <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; okToValidate <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; !IsValidValue(oldCurrentRow,
oldCurrentCol, val.ToString())<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MessageBox.Show("Entry Error");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; okToValidate = false;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dataGridParts.CurrentCell = new
DataGridCell(oldCurrentRow, oldCurrentCol);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; okToValidate = true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oldCurrentRow = newCurrentRow;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oldCurrentCol = newCurrentCol;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; catch (Exception ex)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MessageBox.Show(ex.ToString());<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
<br>
<br>
[quoted text, click to view]
<blockquote cite="mid%23Kw8BnZ9FHA.808@TK2MSFTNGP09.phx.gbl" type="cite">
<pre wrap="">Hi,

[quoted text, click to view]
</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&lt;rowCount)?dataGrid1[oldCurrentRow,
oldCurrentCol]:null;

if ( (val != null) &amp;&amp; (val != DBNull.Value) &amp;&amp;
(oldCurrentRow &lt; rowCount) &amp;&amp;
okToValidate &amp;&amp;
!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.

[quoted text, click to view]
</pre>
<blockquote type="cite">
<pre wrap="">Hi,

[quoted text, click to view]
</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:

Stanislav Nedelchev
11/30/2005 1:31:57 PM
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());

}

}



[quoted text, click to view]
AddThis Social Bookmark Button