Sql 将不同列中的两个值合并到Datagridwiev中的一列中
我正在使用MS SQL Server和Visual Basic 我想将一个计算列和另一个列合并,以向计算中添加单位。(例如“200平方米”)Sql 将不同列中的两个值合并到Datagridwiev中的一列中,sql,sql-server,vb.net,datagridview,foreach,Sql,Sql Server,Vb.net,Datagridview,Foreach,我正在使用MS SQL Server和Visual Basic 我想将一个计算列和另一个列合并,以向计算中添加单位。(例如“200平方米”) 此代码可执行此操作,但仅适用于第一次输入的单位?如何为每个列执行此操作?您可以使用CellFormatting事件 Private WithEvents DgwNewLayout As DataGridView Private Sub DgwNewLayout_CellFormatting(sender As Object, e As DataG
此代码可执行此操作,但仅适用于第一次输入的单位?如何为每个列执行此操作?您可以使用
CellFormatting
事件
Private WithEvents DgwNewLayout As DataGridView
Private Sub DgwNewLayout_CellFormatting(sender As Object,
e As DataGridViewCellFormattingEventArgs
) Handles DgwNewLayout.CellFormatting
If e.ColumnIndex = 5 Then
e.Value = CDbl(e.Value).ToString("0.00") &
" " & CStr(DgwNewLayout.Rows(e.RowIndex).Cells(9).Value)
e.FormattingApplied = True
End If
End Sub
正如Dom Sinclair所说,这可能会导致性能低下。您是否真的在所有数据网格中循环,以组合您想要组合的值?但更好的做法是在数据库本身中执行此操作。创建一个视图或存储过程,以生成所需样式的数据输出,然后将网格视图绑定到该样式。我理解您的担忧,但我不会处理大量数据。为了提高效率,我将尝试一个存储过程。你能给我举个例子吗。(Column1,Column2到Column3)
Private Sub Unite_Transfer()用于DgwNewLayout.Rows Dim n中的每一行,作为整数n=n+1 DgwNewLayout.Item(5,n-1)。Style.Format=“0.00”和“”&DgwNewLayout.Item(9,n-1)。值和“”
Next End Sub`我用过这个。我无法将您的代码应用到第(5)列。但是谢谢你的回答。你能解释一下如何正确使用你的代码吗?我已经扩展了上面的代码示例。现在只格式化了第5列。将句柄
与事件一起使用,另请参见。
Private WithEvents DgwNewLayout As DataGridView
Private Sub DgwNewLayout_CellFormatting(sender As Object,
e As DataGridViewCellFormattingEventArgs
) Handles DgwNewLayout.CellFormatting
If e.ColumnIndex = 5 Then
e.Value = CDbl(e.Value).ToString("0.00") &
" " & CStr(DgwNewLayout.Rows(e.RowIndex).Cells(9).Value)
e.FormattingApplied = True
End If
End Sub