错误:不支持指定的方法?

时间:2010-03-11 04:53:47

标签: c# notsupportedexception

当我尝试调用Find()

时,我一直收到此错误
public void findTxt(string text)
    {
        BindingSource src = new BindingSource();
        src.DataSource = dataGridView1.DataSource;
        src.Position = src.Find("p_Name", text);    // Specified method is not supported

        if (src.Position == 0 && dataGridView1.Rows[0].Cells[2].Value.ToString() == text)
        {
            MessageBox.Show("Item found!!");
            dataGridView1.CurrentCell = dataGridView1.Rows[src.Position].Cells[2];
        }
        else if (src.Position == 0 && dataGridView1.Rows[0].Cells[2].Value.ToString() != text)
        {
            MessageBox.Show("Item not found!!");
        }
        else
        {
            MessageBox.Show("Item found!!");
            dataGridView1.CurrentCell = dataGridView1.Rows[src.Position].Cells[2];
        }

    }

编辑:

从另一个表单调用findText方法时出现错误,但是从主窗体调用此方法不会导致这样的错误。

2 个答案:

答案 0 :(得分:2)

由底层数据源决定它支持哪些操作。我相信DataTable开箱即用的唯一支持此功能。你可以通过以下方式检查(在这种情况下):

IBindingListView blv = yourDataSource as IBindingListView;
bool canSearch = blv != null && blv.SupportsSearching;

因此;什么是底层数据源? List<T>(或甚至BindingList<T>)不会提供此功能。

答案 1 :(得分:0)

我的Asp.Net Core API中出现此错误。这是因为Asp.Net Framework和.Net Core在API方面存在差异。我的应用程序位于Asp.Net Framework中,并且已将其迁移到.Net Core。以下代码在编译时始终可以正常运行,但是在运行时失败,并抛出错误System.NotSupportedException: 'Specified method is not supported.'

Request.Body.Seek(0, SeekOrigin.Begin);
var streamReader = new StreamReader(Request.Body);
bodyData = await streamReader.ReadToEndAsync();

enter image description here

要解决此问题,您要做的就是按照以下正确方法进行更改。

bodyData = await new StreamReader(Request.Body, Encoding.Default).ReadToEndAsync();

您还应该添加System.Text命名空间。

希望有帮助。