How to find an index to the DataView whewn I sort on Country

Discussion in 'Microsoft C# .NET' started by Tony, Dec 30, 2011.

  1. Tony

    Tony Guest

    This is just an example for leaning why not things work as I expect.
    I want to find the index entry in the DataView collection to the ContactName
    = Aria Cruz when we sort on Country ?

    I could do the following.

    protected void Page_Load(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(connectionString);
    string strSQL = "select CustomerID, CompanyName,ContactName, Phone,
    City, Country from Customers";
    SqlDataAdapter da = new SqlDataAdapter(strSQL, con);

    DataTable tbl = new DataTable("Customers");
    da.Fill(tbl);

    DataView dv = new DataView(tbl);

    dv.Sort = "ContactName";
    int indexToContactname = dv.Find("Aria Cruz");
    DataRow rowToFind = dv[indexToContactname ].Row();

    DataView vueByCountry = new DataView(tbl);
    datavjuCountry.Sort = "Country";
    string strCountry = rowToFind["Country"].ToString();

    foreach(DataRowView myRowView in vueByCountry)
    {
    if (myRowView .Row == rowToFind)
    {
    ...
    break;

    }
    }
    }

    I have made another attemt to find a solution to find the index entry in the
    DataView collection to the ContactName = Aria Cruz when we sort on Country
    but that doen not work as expected and I hope somebody can explain why.
    Here is my attempt to this solution
    I first find the DataRowView object called findme in the DataView when I
    sort on ContactName.
    Then I create a second DataView that I sort on Country. Now I use the
    IndexOf method in the interface IList to locate the object findme in the new
    DateView datavjuCountry but this will not give a match and I can't
    understand why ?

    protected void Page_Load(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(connectionString);
    string strSQL = "select CustomerID, CompanyName,ContactName, Phone,
    City, Country from Customers";
    SqlDataAdapter da = new SqlDataAdapter(strSQL, con);

    DataTable tbl = new DataTable("Customers");
    da.Fill(tbl);

    DataView dv = new DataView(tbl);

    dv.Sort = "ContactName";
    int index = dv.Find("Aria Cruz");
    DataRowView findme = dv[index];

    DataView datavjuCountry = new DataView(tbl);
    datavjuCountry.Sort = "Country";


    int slask = ((IList)datavjuCountry).IndexOf(findme);
    }




    //Tony
     
    Tony, Dec 30, 2011
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter S. Guild

    Dataview.Sort = ""; Doesn't work or is it me?

    Peter S. Guild, Oct 27, 2003, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    401
    Peter S. Guild
    Oct 27, 2003
  2. Guest
    Replies:
    1
    Views:
    482
    Marko ®nidar
    Oct 11, 2004
  3. Guest

    DataView index from DataTable index

    Guest, Nov 5, 2004, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    352
    Guest
    Nov 5, 2004
  4. Richard
    Replies:
    5
    Views:
    425
    Bernie Yaeger
    Jul 17, 2005
  5. Ronald S. Cook
    Replies:
    1
    Views:
    327
    Ronald S. Cook
    Jul 7, 2007
Loading...

Share This Page