2010-04-16 2 views
2

DataGridView를 새로 고친 후 사용자의 위치로 돌아가려면 BindingSource.Find()를 사용하고 있습니다. BindingSource.Find() 및 RowID를 검색 대상 DataColumn으로 사용합니다. 불행하게도 Oracle은 대소 문자 만 다른 두 개의 RowID를 반환 할 수 있습니다.BindingSource.Find 키 비교에서 대/소문자를 구분하지 않습니까?

BindingSource.Find()는 대/소문자를 구분하지 않고 첫 번째 일치 항목을 반환합니다.

은 MSDN의 문서에서 상대 :

public int Find(string propertyName, Object key) 

이 프롭퍼티 비교는 대소 문자를 구분하지만, 키 비교가 있는지 여부를 언급하지 않는 것을 말한다.

누구든지 BindingSource.Find 대소 문자를 구분하는 방법을 알고 있습니까?

+0

아직이 솔루션을 찾을 수 없었습니다. 문서를 조금 더 다음 : DataView (나는 내 ​​기본 목록 믿습니다)이 IBindingList.Find를 호출합니다. 대/소문자를 구분하는지 여부는 알 수 없지만 사용자 지정 검색을 사용하는 클래스를 만드는 예가 있습니다. http://msdn.microsoft.com/en-us/library/system.componentmodel.ibindinglist.find.aspx 이 예제를 사용하여 대소 문자가 아닌 DataView에 대한 사용자 지정 검색을 만들 수 있어야합니다. 민감하지만, 어디서부터 시작해야할지 모르겠다. 누구나 접근법을 제안 할 수 있습니까? – shindigo

답변

1

DataView의 경우 DataTableCaseSensitive 속성을 true로 설정하면됩니다. A는 대소 문자를 구분하는 찾기 구현하는 기본 목록을 당신이 필요로하는 IBindingList의 다른 유형의

public Form1() 
{ 
    InitializeComponent(); 

    DataSet set1 = new DataSet(); 

    // Some xml data to populate the DataSet with. 
    string testXml = 
    "<?xml version='1.0' encoding='UTF-8'?>" + 
    "<numbers>" + 
    "<number><name>one</name></number>" + 
    "<number><name>two</name></number>" + 
    "<number><name>Two</name></number>" + 
    "<number><name>three</name></number>" + 
    "</numbers>"; 

    // Read the xml. 
    StringReader reader = new StringReader(testXml); 
    set1.ReadXml(reader); 

    // Get a DataView of the table contained in the dataset. 
    DataTableCollection tables = set1.Tables; 
    // Set the CaseSensetive property 
    tables[0].CaseSensitive = true; 
    DataView view1 = new DataView(tables[0]); 

    // Create a DataGridView control and add it to the form.    
    dataGridView1.AutoGenerateColumns = true;    

    // Create a BindingSource and set its DataSource property to 
    // the DataView. 
    BindingSource source1 = new BindingSource(); 
    source1.DataSource = view1; 

    // Set the data source for the DataGridView. 
    dataGridView1.DataSource = source1; 

    // Set the Position property to the results of the Find method. 
    int itemFound = source1.Find("name", "Two"); 
    source1.Position = itemFound; 
} 

을, 워드 프로세서 말하는대로 : 난 그냥 빨리 다음 코드를 사용하는 것이 프로토 타입과 잘 작동합니다. 완성도를 위해 내가 아래에이 작업을 수행하는 코드를 보여 주었다 :

public Form1() 
{ 
    InitializeComponent(); 

    // This uses my CaseSensitiveBindingList which I have code for later 
    BindingList<DGVItems> source = new CaseSensitiveBindingList<DGVItems>() 
     { 
      new DGVItems { Number = "one" }, 
      new DGVItems{Number = "two"}, 
      new DGVItems{Number = "Two"} 
     }; 

    BindingSource bindingSource = new BindingSource(); 
    bindingSource.DataSource = source; 

    dataGridView1.DataSource = bindingSource; 

    var index = bindingSource.Find("Number", "Two"); 

    // Index is 2 (third item) as desired. 
    MessageBox.Show(number.ToString()); 

} 

public class DGVItems 
{ 
    public string Number { get; set; } 
} 

그리고 CaseSensitiveBindingList에 대한 코드입니다 :

public class CaseInsensitiveBindingList<T> : BindingList<T> 
{ 
    protected override int FindCore(PropertyDescriptor prop, object key) 
    { 
     string stringKey = key as string;    

     bool keyIsString = stringKey != null; 

     for (int i = 0; i < Count; ++i) 
     { 
      if (keyIsString && prop.PropertyType.IsAssignableFrom(typeof(string))) 
      { 
       if (stringKey.Equals(prop.GetValue(Items[i]).ToString(), StringComparison.CurrentCulture)) 
        return i; 
      } 
      else 
      { 
       if (key.Equals(prop.GetValue(Items[i]))) 
        return i;  
      } 

     } 

     return -1; 
    } 
} 

그 코드는 거의 확실하게 개선하지만, 일반적인 개념을 표시 할 수있다.

+0

우수! 전체 예제를 가져 주셔서 감사합니다. 나는 아직 그들을 시험하지는 않았지만 그들은 견고 해 보인다. – shindigo

관련 문제