Iterate through Datagrid

Hello,

Does anyone have a code example they can post, of Iterating through a datagrid to check a cell content.
I really do not know where to start on this one.

Thanks
Peter


Answer this question

Iterate through Datagrid

  • Shambhu

    Thanks rkimble,

    What I really want to do is just move the selection to the record below, not the record itself.

    Instead of manually clicking the row with the mouse to move around the selected record, I was hoping to have a button that when it is clicked the selected row becomes the row below.

    So as the button is clicked, the highlight that appears when a row is selected, moves down the datagrid.

    Thanks
    Peter

  • Yazeedhs

    Thanks alot rkimble,

    Your solution works great.

    Thanks again
    Patere

  • JohnFx

    Oh, ok, well that is easier then...  =)

    Just do the following in the Button.Click event:



    Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember)

    If bmb.Position < bmb.Count - 1 Then

    bmb.Position += 1

    Else

    bmb.Position = 0

    End If


    This will advance the position to the next record, or back to the first record if your at the last.

  • cgarfer

    Thanks Bloke,

    I am trying to achieve what would appear to be an easy thing,

    I have a button, when the user clicks the button, I need the selected row in the datagrid to move below to the next row, so if the user keeps pushing the button, the selected row of the datagrid keeps moving to the row below.

    this is what I have so far, but could be completely off the right track.


    Dim cm As CurrencyManager = Me.BindingContext(DGjobrequest.DataSource, DGjobrequest.DataMember)
            Dim dv As DataView = CType(cm.List, DataView)
            Dim i As Integer
            For i = 0 To dv.Count - 1
                DGjobrequest.Select(i - 1)
            Next i
    </Code>

    Thanks
    Peter

  • Michael Barton

    Oh, here's an edit that also updates the colored selection bar in the datagrid:


    Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember)

    'Store the current position 
            Dim old As Integer = bmb.Position

            If bmb.Position < bmb.Count - 1 Then

                bmb.Position += 1

            Else

                bmb.Position = 0

            End If

    'Unselect the last row
            DataGrid1.UnSelect(old)
    'Select the new row
            DataGrid1.Select(bmb.Position)



  • mikheil

    No problem.  Glad it helped!
  • Niel-O

    It may not be impossible to iterate through the cells but the easiest way is to iterate through the datatable which is the DataGrids datasource.

    MyDataGrid.DataSource = MyDataSet.MyDataTable

    ...

    foreach(DataRow dr in MyDataSet.MyDataGridTable.Rows)
    {
       //do something with dr
       MessageBox.Show("Customer Name is " + dr.CustomerName);
    }

    Life can get a lot more complicated but this would be a good place to start.
     

  • kezhu

    Hi,
     U can try this............

     dgDocumentList.CurrentRowIndex = intRowNo

    and u can access the cell value as

    dgDocumentList.Item(intRowNo, 1)


    I hope this will help u................

    Regards,
    Ritesh

  • lfranklin

    Peter,

    This does not appear to be an easy thing...

    The order of rows in a DataGrid is determined by the SQL query that fills the underlying DataTable, or by a DataView object's Sort property.  You cannot (or should not) arbitraly try to change the order of rows in DataGrid.  You need to change the values you sort on, per record, or change the sort order.

    Your table should be sorted by some column.  If you want to change a row's position in the table, you'll need to modify the value of that record for the column you sort on.  So if you have a two column table with "Number" and "Name" as columns and the following data:

    1  Blue
    2  Red
    3  Orange
    4  Green

    And you want to move "Red" below "Orange", you need to update the two records so that Red's number is 3 and Orange's number is 2.

    It might help for you to explain what you are trying to achieve...

  • Iterate through Datagrid