Loop through datagrid (search)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Loop through datagrid (search)

    Hi there. i got the code for a type of search, incremental if im not mistaken but when i press find, it finds the first instance of what was typed in the textbox but i want it to be like, If you press find again it takes you to the next row containing the same value.
    any help?

    Here is my code:

    private int GetRowHandleByC olumnValue(Grid View view, string ColumnFieldName , object value)
    {
    string columnName = ColumnFieldName ;
    int result = GridControl.Inv alidRowHandle;
    for (int i = 0; i < view.RowCount; i++)
    if (view.GetDataRo w(i)[columnName].Equals(value))
    return i;
    return result;
    }

    private void btnFind_Click(o bject sender, System.EventArg s e)
    {
    int rowHandle = GetRowHandleByC olumnValue(gvAs sets, "AST__CODE" , txtFind.Text);
    if (rowHandle != GridControl.Inv alidRowHandle)
    {
    gvAssets.Focuse dColumn = gvAssets.Column s.ColumnByField Name("AST__CODE ");
    gvAssets.Focuse dRowHandle = rowHandle;
    if (gvAssets.IsRow Visible(rowHand le) == RowVisibleState .Hidden)
    gvAssets.MakeRo wVisible(rowHan dle, false);
    gvAssets.ShowEd itor();
    }
    else
    MessageBox.Show ("Not found!");
    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Code:
    for (int i = 0; i < view.RowCount; i++)
                    if (view.GetDataRow(i)[columnName].Equals(value))
                        return i;
    If you always start your search at 0, then you will always get the first result.
    You need to alter the search in such a way that it starts at 'LeftOffAt" int. To start from the beginning, set 'LeftOffAt' to zero. To continue searching from where you LeftOffAt.... you get the picture.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      By the way... Please start wrapping your code blocks in code tags.

      Comment

      Working...