Re: DataGrid problem that's very difficult. A Challenge for someone

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Cowboy \(Gregory A. Beamer\) (NoSpamMgbworld_at_comcast.netNoSpamM)
Date: 04/26/04


Date: Mon, 26 Apr 2004 09:53:39 -0500

When you change the data, rebind the data. If you allow the caching to keep
the original row count, you will end up with problems. In an ASP.NET
application, repulling from the database is easier than rebinding to a
cached DataSet (it also cleans things up nicely). In WindowsForms, you can
go either route, as the client-server model is a bit more flexible (stateful
versus stateless).

-- 
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
**********************************************************************
Think Outside the Box!
**********************************************************************
"GarrySymes" <garry@futuredata.com> wrote in message
news:1CDF29A1-75E4-42C5-A451-3FFE59797CED@microsoft.com...
> This is a difficult problem and I have been trying everything for a week
or more.
> It is a datagrid on a form and a button for adding a row and a button for
deleting a row that gets enabled when a row is selected. The datagrid is
bound to an array list rather than directly to a database.
> If the add button is clicked, a row gets added ok and the program moves to
a cell on the new row ready for editing. I then select the row by clicking
in the left area by the row (which highlights the row) and click the delete
button. The row does not get validated becasue I set the "Cause Validation"
to false for the button and then the function runs (code below).
> PROBLEM. I move the CurrentRowIndex off the row that I want to delete by
doing:-
> DataGridProductType1.CurrentRowIndex =
DataGridProductType1.CurrentRowIndex - iwkNoOfRowsSelected
> where I have confirmed that iwkNoOfRowsSelected is 1.
> When debugging I find that after this line of code the CurrentRowIndex has
not changed, it has not been decremented by 1. Because of this, when I have
actually deleted the row in the arraylist and return to the grid I get an
exception stating that an index was out of bounds. This exception is not in
my code but in the .Net code somewhere.
> I guess, because I was not able to move off the current row before
deleting the row, when the grid is redrawn it finds the currentrowindex is
not there anymore!!!
>
> HELP Has anyone got any ideas at all.
>
>
>
> Private Sub ButtonDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
>             Handles ButtonDelete.Click
>     Dim iwkRow As Integer
>     Dim iwkNoOfRowsSelected As Integer
>     Dim swkDescriptionString As String
>     Dim CwkArrayList As ArrayList
>     ' Assumes the DataGrid is bound to an ArrayList.
>     CwkArrayList = DirectCast(DataGridProductType1.DataSource, ArrayList)
>
>     '//***********************************************************
>     '// Count the number of items selected.
>     '//***********************************************************
>     iwkNoOfRowsSelected = RowHowManySelected(DataGridProductType1)
>
>      '//***********************************************************
>     '// Move the current row index
>     '//***********************************************************
>     If iwkNoOfRowsSelected >= DataGridProductType1.CurrentRowIndex Then
>       DataGridProductType1.CurrentRowIndex = 0
>     Else
>       DataGridProductType1.CurrentRowIndex =
DataGridProductType1.CurrentRowIndex - iwkNoOfRowsSelected
>     End If
>     '//***********************************************************
>     '// Go through Grid rows and delete ALL items that are selected
>     '//***********************************************************
>     For iwkRow = CwkArrayList.Count - 1 To 0 Step -1
>       If DataGridProductType1.IsSelected(iwkRow) Then
>         '//***********************************************************
>         '// Delete this row
>         '// Get key field text and then use that to delete the item
>         '//***********************************************************
>         swkDescriptionString = DataGridProductType1(iwkRow, 0)
>         m_ClassProductType1Control.Delete(swkDescriptionString)
>         '// alternate way of deleteing a row
>         'm_CBindingManagerBase.RemoveAt(iRow)
>       End If
>     Next iwkRow
>
>     '//***********************************************************
>     '// Refresh the data grid
>     '//***********************************************************
>     DataGridProductType1.DataSource = Nothing
>     DataGridProductType1.DataSource =
m_ClassProductType1Control.GetArrayList()
>   End Sub
>

Quantcast