vb.net 如何使用查询向 datagridview 中的单元格添加值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17755759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add value to a cell in a datagridview using a query?
提问by David
I have a bound datagridview that has an unbound column. I'm using cellformatting to look up the Employee ID in the same row to return the employee's name from a different datatable.
我有一个绑定的 datagridview,它有一个未绑定的列。我正在使用 cellformatting 在同一行中查找员工 ID,以从不同的数据表中返回员工的姓名。
I get everything working fine, the only problem is that i can only get one part of the name to show either the first name, middel, last, but not all together.
我一切正常,唯一的问题是我只能让名称的一部分显示名字、中间名、姓氏,但不能全部显示。
This is the code i have so far (I'm very new to coding, all that i have so far has been from forums or research only, so please comment your answer):
这是我到目前为止的代码(我对编码很陌生,到目前为止我所拥有的只是来自论坛或研究,所以请评论您的答案):
If dgvr.Cells(0).Value IsNot Nothing AndAlso dgvr.Cells(0).Value IsNot DBNull.Value Then
Dim empID As Integer = CInt(dgvr.Cells(0).Value)
Dim qry = From dr As PersonalObraDataSet.PersonalObRow In _PersonalObraDataSet.PersonalOb _
Where (dr.cdTrabajador = empID)
'If qry.Count > 0 Then
DataGridView1.Rows(e.RowIndex).Cells(1).Value = qry.First.Apellido1
'DataGridView1.Rows(e.RowIndex).Cells(2).Value = qry.First.Nombre2
End If
'End If
回答by Yuriy Galanter
You can combine strings with & operator. It would be something like
您可以使用 & 运算符组合字符串。它会是这样的
DataGridView1.Rows(e.RowIndex).Cells(1).Value = qry.First.Nombre2 & " " & qry.First.Apellido1 & " " & qry.First.SegundoNombre3

