Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / November 2005

Tip: Looking for answers? Try searching our database.

DataGrid null values !

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stanislav Nedelchev - 29 Nov 2005 09:10 GMT
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 - 29 Nov 2005 15:55 GMT
Hi,

> 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

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 - 30 Nov 2005 06:55 GMT
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.

> Hi,
>
[quoted text clipped - 40 lines]
> HTH,
> Greetings
Bart Mermuys - 30 Nov 2005 10:33 GMT
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>
&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>
<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&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.

   </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 ) &amp;&amp; ( val != null ) &amp;&amp;
       ( okToValidate ) &amp;&amp;
       ( !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]
>
>  

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.