Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
最佳重载方法匹配。。。。c#数据边界项_C#_Database_Visual Studio 2010_Datagridview - Fatal编程技术网

最佳重载方法匹配。。。。c#数据边界项

最佳重载方法匹配。。。。c#数据边界项,c#,database,visual-studio-2010,datagridview,C#,Database,Visual Studio 2010,Datagridview,我正在尝试将所选行从datagridview1(form1)传递到datagridview1(Form4),这是我的代码列表。但我遇到了错误。由于我的编程技能不是很好,如果你能澄清问题,请详细解释。。。谢谢 if (tableListBox.SelectedIndex == 2) { List<string> sendingList = new List<string>(); foreach

我正在尝试将所选行从datagridview1(form1)传递到datagridview1(Form4),这是我的代码列表。但我遇到了错误。由于我的编程技能不是很好,如果你能澄清问题,请详细解释。。。谢谢

        if (tableListBox.SelectedIndex == 2)
        {
            List<string> sendingList = new List<string>();
            foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
            {
                int counter = 0;
                sendingList.Add(dr.DataBoundItem);// The best overload method match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid argument

            }
            Form4 form4 = new Form4(sendingList);
            form4.Show();

        }
if(tableListBox.SelectedIndex==2)
{
列表发送列表=新列表();
foreach(dataGridView1.SelectedRows中的DataGridViewRow dr)
{
int计数器=0;
sendingList.Add(dr.DataBoundItem);//与“System.Collections.Generic.List.Add(string)”匹配的最佳重载方法具有一些无效参数
}
Form4 Form4=新的Form4(发送列表);
表4.Show();
}

您需要将列表的类型更改为对象,或者将对象转换为字符串(使用“dr.DataBoundItem as string”)。SendingList是一个字符串列表,因此如果不先对其进行转换,则无法向其中添加对象

要将对象转换为字符串(假设该字符串已转换为对象),请执行以下操作:


出现该错误的原因是您的类型不匹配。如果你看一下,你会发现它的定义如下

public Object DataBoundItem { get; }
这意味着返回类型为
Object
。该错误是因为
List.Add()
方法希望参数在您的案例中为T类型
List.Add(string)
。列表应为DataBoundItem可以强制转换到的类型。查看“帮助”页面中的示例

void invoiceButton_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
    {
        Customer cust = row.DataBoundItem as Customer;
        if (cust != null)
        {
            cust.SendInvoice();
        }
    }
}

DataBoundItem被强制转换为Customer对象。如果您想将它们捕获到一个列表中,它将是一个
列表
。您也可以使用
列表
,但最好是对象。

提示:您试图在
字符串
列表中存储不是
字符串的内容。也许可以尝试将
DataBoundItem
转换为
字符串
?@SimonWhitehead,我该怎么做?对不起,我对编程很陌生。。。你有什么我可以参考的网站吗?谢谢
void invoiceButton_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
    {
        Customer cust = row.DataBoundItem as Customer;
        if (cust != null)
        {
            cust.SendInvoice();
        }
    }
}