2010-04-16 111 views
2

我使用BindingSource.Find()在刷新DataGridView后返回用户的位置。我使用BindingSource.Find()和一个RowID作为我正在搜索的DataColumn。不幸的是,Oracle可以返回两个不同的RowID。BindingSource.Find键比较是不区分大小写的?

BindingSource.Find()返回第一个匹配,无论大小写。

望着MSDN文档:

public int Find(string propertyName, Object key) 

它说,propertyName的比较是不区分大小写的,但没有提及键比较是否是。

有谁知道如何使BindingSource.Find区分大小写?

+0

我仍然无法找到这个解决方案。在文档之后稍微进一步:对于DataView(我相信它是我的基础列表),它表示这称为IBindingList.Find。没有关于这是否区分大小写的说法,但是有一个创建具有自定义搜索的类的示例。 http://msdn.microsoft.com/en-us/library/system.componentmodel.ibindinglist.find.aspx 我希望我应该能够使用此示例来创建DataView的自定义搜索,而不是大小写敏感,但是,我不知道从哪里开始。 任何人都可以提出一种方法吗? – shindigo 2010-04-20 13:30:37

回答

1

对于DataView您需要做的就是将父DataTableCaseSensitive属性设置为true。我只是快速原型与下面的代码,它工作正常:

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; 
} 

对于需要其他类型的IBindingList的,作为文档说,它实现了一个查找区分大小写的基础列表。对于完整性我展示的代码做下面这个样子:

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 2011-04-11 17:35:00