在DataColumn上使用BindingSource使用'SUBSTRING(expression,startIndex,length)' [英] Using 'SUBSTRING(expression, startIndex, length)' with BindingSource on DataColumn

查看:184
本文介绍了在DataColumn上使用BindingSource使用'SUBSTRING(expression,startIndex,length)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过匹配源字符串的开头和搜索字符串的长度来过滤数字字符串。



所有过滤的列都是字符串。大多数只是单词,但是有一列具有如下这些 001302:Alt#的值。底层的数据源很烂,但我不能改变结构。根据左侧值(即 050110 050534 都与客户端(05)相关, , 052123 与客户(05)发票(2)有关)。我的用户希望能够搜索DataGridView中的数据并按组过滤(即输入05,并查看以05开始的所有内容,或052,参见052000-052999)


$ b $我正在生成过滤器字符串,并使用DataSet作为DataSource并将Table设置为BindingSource上的Filter属性作为DataMember。我的其他过滤工作,我唯一的问题是与数字过滤器。当我设置 BindingSource.Filter 属性时,它会抛出一个 ArgumentOutOfRange 异常。


System.Data.dll中出现类型为System.ArgumentOutOfRangeException
的未处理异常其他信息:Substring()
参数超出范围。


我以为我可能会对一个空行发射,或者只有几个数字,但是它只是射击一个字符因此,length参数应该是有效的,但它仍然抛出异常。

  bool firstColumn = true; 
foreach(FilteredColumns.Keys中的DataColumn列)
{
if(FilteredColumns [column])
{
if(!firstColumn)fieldFilter.Append()OR (); else firstColumn = false;

int numericFilter;
if(int.TryParse(filterWord,out numericFilter))
{
fieldFilter.Append(String.Format(SUBSTRING({0},0,{2})= {1} ,column.ColumnName,filterWord,filterWord.Length));
}
else
{
fieldFilter.Append(String.Format({0} like'%{1}%',column.ColumnName,filterWord));
}
}
}

我相信我只是缺少一些愚蠢的东西我非常感谢你指出我的错误!

解决方案

没关系。我假设DataColumn表达式SUBSTRING与String.Substring的行为相同。但是SUBSTRING表达式是基于1的索引,而String.Substring方法将其索引开始为0.我应该先检查一下,而是尝试运行到砖墙几个小时...



谢谢反正家伙!


I am trying to filter a numeric string by matching the start of the source string and the length of the search string.

All the filtered columns are strings. Most are just words, but there is one column that has values that look like this 001302:Alt#. The underlying data source sucks, but I cannot change the structure. The numbers are grouped based on the left-hand values (i.e. 050110 and 050534 are both related to client(05), 052123 is related to client(05) invoices(2)). My users want to be able to search the data in the DataGridView and filter that column by group (i.e. Enter 05 and see everything that starts with 05, or 052 and see 052000-052999)

I am generating the filter string and setting the Filter property on the BindingSource using the DataSet as DataSource and the Table as DataMember. The rest of my filtering works, the only problem I am having is with the numeric filter. When I set the BindingSource.Filter property, it throws an ArgumentOutOfRange exception.

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Data.dll Additional information: Substring() argument is out of range.

I thought I might be firing this against an empty row, or one that only had a few digits, but it is firing with only one character. Therefore, the length argument should be valid, but it is still throwing the exception.

bool firstColumn = true;
foreach (DataColumn column in FilteredColumns.Keys)
{
    if (FilteredColumns[column])
    {
        if (!firstColumn) fieldFilter.Append(") OR ("); else firstColumn = false;

        int numericFilter;
        if (int.TryParse(filterWord, out numericFilter))
        {
            fieldFilter.Append(String.Format("SUBSTRING({0}, 0, {2}) = {1}", column.ColumnName, filterWord, filterWord.Length));
        }
        else
            {
            fieldFilter.Append(String.Format("{0} like '%{1}%'", column.ColumnName, filterWord));
            }
    }
}

I am sure I am just missing something stupid. I thank you in advance for pointing out my error(s)!

解决方案

Never mind. I was making the assumption that the DataColumn Expression SUBSTRING behaved the same as String.Substring. However the SUBSTRING Expression is a 1 based index, whereas the String.Substring method starts it's index at 0. I should have checked that first, but instead tried running into a brick wall for hours...

Thanks anyways guys!

这篇关于在DataColumn上使用BindingSource使用'SUBSTRING(expression,startIndex,length)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆