i am trying to pass selected row from datagridview1 (form1) to datagridview1(form 4) and this is my list of codes..but i am getting error. As my programming skill isn't very well, do explain in details if you can clarify the problem... thanks.
我试图将选定的行从datagridview1(form1)传递到datagridview1(表单4),这是我的代码列表..但我收到错误。由于我的编程技巧不是很好,如果你能澄清问题,请详细解释......谢谢。
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();
}
2 个解决方案
#1
0
You need to either change the type of your list to objects, or convert your object to a string (using 'dr.DataBoundItem as string'). SendingList is a list of strings, so you can't add an object to it without converting it first.
您需要将列表类型更改为对象,或将对象转换为字符串(使用'dr.DataBoundItem as string')。 SendingList是一个字符串列表,因此您无法在不首先转换对象的情况下向其添加对象。
To convert the object to a string (assuming it is a string that was converted to an object):
要将对象转换为字符串(假设它是转换为对象的字符串):
sendingList.Add(dr.DataBoundItem as string);
#2
0
The reason you are getting that error is because your type doesn't match. If you look at DataGridViewRow.DataBoundItem you can see that it is defined as follows.
您收到该错误的原因是您的类型不匹配。如果查看DataGridViewRow.DataBoundItem,您可以看到它的定义如下。
public Object DataBoundItem { get; }
This means that the return type is Object
. The error is because the List<T>.Add()
method expects the parameter to be of type T in your case List<string>.Add(string)
. The list should be the of the type that DataBoundItem can be cast into. Looking at the example in the help page...
这意味着返回类型是Object。该错误是因为List
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();
}
}
}
The DataBoundItem is cast as a Customer object. And if you wanted to capture them into a list it would be a List<Customer>
. You could also use a List<object>
however it is preferred that objects are strongly typed.
DataBoundItem被强制转换为Customer对象。如果您想将它们捕获到列表中,那么它将是List
#1
0
You need to either change the type of your list to objects, or convert your object to a string (using 'dr.DataBoundItem as string'). SendingList is a list of strings, so you can't add an object to it without converting it first.
您需要将列表类型更改为对象,或将对象转换为字符串(使用'dr.DataBoundItem as string')。 SendingList是一个字符串列表,因此您无法在不首先转换对象的情况下向其添加对象。
To convert the object to a string (assuming it is a string that was converted to an object):
要将对象转换为字符串(假设它是转换为对象的字符串):
sendingList.Add(dr.DataBoundItem as string);
#2
0
The reason you are getting that error is because your type doesn't match. If you look at DataGridViewRow.DataBoundItem you can see that it is defined as follows.
您收到该错误的原因是您的类型不匹配。如果查看DataGridViewRow.DataBoundItem,您可以看到它的定义如下。
public Object DataBoundItem { get; }
This means that the return type is Object
. The error is because the List<T>.Add()
method expects the parameter to be of type T in your case List<string>.Add(string)
. The list should be the of the type that DataBoundItem can be cast into. Looking at the example in the help page...
这意味着返回类型是Object。该错误是因为List
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();
}
}
}
The DataBoundItem is cast as a Customer object. And if you wanted to capture them into a list it would be a List<Customer>
. You could also use a List<object>
however it is preferred that objects are strongly typed.
DataBoundItem被强制转换为Customer对象。如果您想将它们捕获到列表中,那么它将是List