最佳重载方法匹配。。。。c#数据边界项
我正在尝试将所选行从datagridview1(form1)传递到datagridview1(Form4),这是我的代码列表。但我遇到了错误。由于我的编程技能不是很好,如果你能澄清问题,请详细解释。。。谢谢最佳重载方法匹配。。。。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
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();
}
}
}