Messed up DataGridView when adding row to sorted datagrid view

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a DataGridView bound to a database Table. Visual Studio created the grid and a binding navigator Toolstrip. Navigating, Insert, Delete are working fine...

BUT:
If I sort the grid by clicking on some column header, inserting a row is messing up the grid. The row index passed to the RowsAdded event handler is wrong. Its always the index of last row which was fine in unsorted mode before. In sorted mode I am editing
the wrong row and see no chance to find out the index of the right one. However, all data bound columns are displayed correctly even though index is always the last row. The problem occurs with not data bound columns. In my case an image column depending on
a data bound column. The image is always drawn in the last row. Thus ignoring sorting...
Here is what I am doing in the RowsAdded event handler:
<pre class="lang-cs prettyprint
Code:
<span class="kwd for<span class="pln  <span class="pun (<span class="kwd int<span class="pln  i <span class="pun =<span class="pln  <span class="lit 0<span class="pun ;<span class="pln  i <span class="pun <<span class="pln  e<span class="pun .<span class="typ RowCount<span class="pun ;<span class="pln  <span class="pun ++<span class="pln i<span class="pun )<span class="pln  
        <span class="pun {<span class="pln  
            <span class="typ DataGridViewRow<span class="pln  row <span class="pun =<span class="pln  dataGridView_Energietraeger<span class="pun .<span class="typ Rows<span class="pun [<span class="pln i <span class="pun +<span class="pln  e<span class="pun .<span class="typ RowIndex<span class="pun ];<span class="pln  
 
            <span class="kwd object<span class="pln  idObj <span class="pun =<span class="pln  row<span class="pun .<span class="typ Cells<span class="pun [<span class="typ Column_Id<span class="pun .<span class="typ Index<span class="pun ].<span class="typ Value<span class="pun ;<span class="pln  
            <span class="kwd if<span class="pln  <span class="pun (<span class="pln idObj <span class="kwd is<span class="pln  <span class="typ System<span class="pun .<span class="typ DBNull<span class="pln  <span class="pun ||<span class="pln  idObj <span class="pun ==<span class="pln  <span class="kwd null<span class="pun )<span class="pln  
            <span class="pun {<span class="pln  
                <span class="com // set default values to all cells in new row...<span class="pln  
 
                <span class="com // set icon indicating ro/ rw mode - wrong row when sorted!<span class="pln  
                row<span class="pun .<span class="typ ReadOnly<span class="pln  <span class="pun =<span class="pln  <span class="pun (<span class="pln bool<span class="pun )<span class="pln readonlyObj<span class="pun ;<span class="pln  
                <span class="typ SetIconCellImage<span class="pun (<span class="pln row<span class="pun );<span class="pln    
            <span class="pun }<span class="pln  
            <span class="kwd else<span class="pln  
            <span class="pun {<span class="pln  
                <span class="kwd object<span class="pln  readonlyObj <span class="pun =<span class="pln  row<span class="pun .<span class="typ Cells<span class="pun [<span class="typ Column_Readonly<span class="pun .<span class="typ Index<span class="pun ].<span class="typ Value<span class="pun ;<span class="pln  
                <span class="kwd if<span class="pln  <span class="pun (!(<span class="pln readonlyObj <span class="kwd is<span class="pln  <span class="typ System<span class="pun .<span class="typ DBNull<span class="pun )<span class="pln  <span class="pun &&<span class="pln  readonlyObj <span class="pun !=<span class="pln  <span class="kwd null<span class="pun )<span class="pln  
                <span class="pun {<span class="pln  
                    row<span class="pun .<span class="typ ReadOnly<span class="pln  <span class="pun =<span class="pln  <span class="pun (<span class="pln bool<span class="pun )<span class="pln readonlyObj<span class="pun ;<span class="pln  
                    <span class="typ SetIconCellImage<span class="pun (<span class="pln row<span class="pun );<span class="pln   <span class="com //<span class="pln  
                <span class="pun }<span class="pln  
            <span class="pun }<span class="pln  
        <span class="pun }<span class="pln
[/code]
Any hints? May be I did not understand logics of databinding and how to access rows and datasources. but I cant see my mistake yet. Thanks in advance... Holger

View the full article
 
Back
Top