How to get the correct value for DataGrid.Select(index) after DataView.Sort

I've read a lot of postings, but still don't seem to get this right. Hopefully someone out there can help.

This is in C#, using Winforms:

I have a datagrid, a combobox, and a textbox all on the same form.

I would like the user to be able to use the combobox to 'select' one of the columns in the datagrid and then enter a search value for that column in the textbox. When the user has entered enough text to match a value in the selected column, the matching row should be hilighted in the datagrid.

In my TextChanged event handler for the textbox I would like to do this:

1. based on the selected value of the combobox, get the corresponding DataGridColumnStyle.MappingName. (I am able to do this, no problem here)

2. Using the value from the textbox, find a matching row. Here's my attempt, note that my DataGrid is bound to a DataSet.

DataView dv = ((DataSet)dg.DataSource).Tables[0].DefaultView;

dv.Sort = strColumnMappingName;

int iRow = dv.Find(strTextBoxValue);

if(iRow>=0 && iRow<dv.Count) dg.Select(iRow); //<-tdg.Select() selects the wrong row.

I have verified that the row index returned from Find() is correct. I did this by adding addition code and using the debugger/breakpoint to drill down to the data row in the DataView

There are several thousand rows in the DataGrid, so forcibly iterating them trying to find a match (on each keystroke) doesn't seem like a good solution.

Could someone help me to understand what I am missing

Thanks



Answer this question

How to get the correct value for DataGrid.Select(index) after DataView.Sort

  • hometoast

    Unfortunately, binding to the DataTable directly causes other problems.

    But I did discover why DataView.Sort wasn't updating the DataGrid; I was getting the wrong DataView. Instead of this:

    DataView dv=((DataSet)dg.DataSource).Tables[0].DefaultView;

    I needed to do this:

    CurrencyManager cm=(CurrencyManager)dg.BindingContext[dg.DataSource,"managers"];

    DataView dv=(DataView)cm.List;

    On a final note. I've given up trying to solve this using v1.1. v2.0 has DataView.ToTable() and DataRowCollection.IndexOf() which I can use as follows

    1. get the (correct) DataView as I've shown above

    2. Use DataView.ToTable() to create a second table, (e.g. "dt2") in the current sort order

    3. use dv2.Search() to get a DataRow (multiple could be found, but I'm only interested in the first one)

    4. dv2.Rows.IndexOf(DataRow) to get the row index in the current sort order

    5. Hilight the row using DataGrid.Select(rowindex)


  • Jordachec

    DataTable dt = new DataTable("mytable");

    //...fill dt from a SqlDataAdapter...

    DataSet set = new DataSet();

    set.Tables.Add(dt);

    dg.SetDataBinding(dt.DataSet,"mytable");


  • Fawad Hamid

    Funny you should mention that. My understanding is that sorting the DataTable's DefaultView should result in the DataGrid being sorted as well, but (at least in my case) it does not. I suppose that if I could figure out how to programmatically sort the grid this problem would be solved.
  • Genti

    Are you sure dv.Sort = strColumnMappingName; sets the column of the DataGrid sorted (just a guess) Try to click on the column header of the DataGrid to explicitly sort the column before you change the TextBox value.

  • MaxAtHome

    If you just want to get the value of a selected cell, then use code as below:

    Assuming you have a data grid named "dgResults":

    dgResults[dgResults.CurrentCell.RowNumber, dgResults.CurrentCell.ColumnNumber]

    This returns an object, so you can call .ToString() to get a string.


  • XavierP

    An easier way to discover selected row:

    int rr = grdContagem.CurrentRowIndex;
    System.Data.DataRowView r = dt.DefaultView[rr];
    System.Data.DataRow row = r.Row;


    Best regards,

    Richter



  • Philip.Mostert

    k.m wrote:
    I suppose that if I could figure out how to programmatically sort the grid this problem would be solved.

    Well, I think there is another way. Let's change a bit the way you bind the DataGrid.

    Replace this line: dg.SetDataBinding(dt.DataSet,"mytable");

    with this one: dg.SetDataBinding(dt,"");

    And in the TextChanged event handler get dv like:

    DataView dv = ((DataTable)dg.DataSource).DefaultView;

    Hope this will help.



  • krisjl20

    From the code you provided I can't see something wrong, but don't forget that previously selected row remains selected unless you call DataGrid.UnSelect(). Can you show how you bind your DataGrid

  • JSteuerIII

    Also have a look here reagarding the indexes:
    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=150726&SiteID=1


  • How to get the correct value for DataGrid.Select(index) after DataView.Sort