提问人:roy 提问时间:4/6/2023 最后编辑:roy 更新时间:4/8/2023 访问量:249
如何手动将列datagridview值转换为字符串?
How to convert column datagridview values to strings manually?
问:
如何在 VB.NET 中手动将列数据网格视图值转换为字符串?
我尝试手动转换值,它不起作用,因为我想在打印预览库中使用它,但如果我使用另一个示例(进一步显示),它可以工作。我的代码有错误吗?
Public Function getData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int32))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("Education", GetType(String))
dt.Columns.Add("Location", GetType(String))
dt.Columns.Add("Price", GetType(Double))
dt.Rows.Add(1, "Satinder Singh", "Bsc Com Sci", "Mumbai", 25000)
dt.Rows.Add(2, "Amit Sarna", "Mstr Com Sci", "Mumbai", 21000)
dt.Rows.Add(3, "Andrea Ely", "Bsc Bio-Chemistry", "Queensland", 35000)
dt.Rows.Add(4, "Leslie Mac", "MSC", "Town-ville", 22000)
dt.Rows.Add(5, "Vaibhav Adhyapak", "MBA", "New Delhi", 27000)
Return dt
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = getData()
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If (e.ColumnIndex = 4) Then
e.Value = String.Format("{0:N2}", e.Value)
End If
End Sub
Private Sub GenerateReport()
KtReport1.Clear()
KtReport1.AddString("<h2>Product List</h2>")
KtReport1.AddHorizontalRule()
ktReport1.AddDatagridView(DataGridView1)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GenerateReport()
KtReport1.ShowPrintPreviewDialog()
End Sub
列价格中的 DataGridView:- 打印预览报告中列价格中的 DataGridView:-
如果我使用另一个例子,它可以工作:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim random As New Random()
For i As Integer = 0 To 4
Dim productName = "Product" & (i + 1)
Dim productPrice = random.Next(5, 5000)
DataGridView1.Rows.Add(New Object() {productName, productPrice.ToString("N2")})
Next i
End Sub
Private Sub GenerateReport()
KtReport1.Clear()
KtReport1.AddString("<h2>Product List</h2>")
KtReport1.AddHorizontalRule()
ktReport1.AddDatagridView(DataGridView1)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GenerateReport()
KtReport1.ShowPrintPreviewDialog()
End Sub
另一个示例结果 DataGridView in 列 Price:-
另一个示例结果 DataGridView in 列 Price in Print Preview Report:-
来自 user09938 的答案结果
答:
0赞
Tu deschizi eu inchid
4/7/2023
#1
目前尚不清楚是什么,但似乎您希望在 DataGridView 中设置列的格式。下面演示如何将 DataTable 与 DataGridView 一起使用,以及如何设置 DataGridViewColumn 属性。KtReport1
Price
它特别适用于计算,例如财务计算, 需要大量数字,但不能容忍舍入错误。
在下面的代码中,您会注意到,如果 DataTable 列的数据类型为 ,则设置为 。否则,不会设置格式。Decimal
Format
N2
形式1.vb
Public Class Form1
'create new instance
Private _dt As DataTable = New DataTable()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'initialize DataTable
InitializeDataTable(_dt)
'initialize DataGridView
InitializeDataGridView(_dt)
'bind to DataTable
DataGridView1.DataSource = _dt
'add test data
AddTestData(_dt)
End Sub
Public Sub InitializeDataTable(dt As DataTable)
'add columns to DataTable
dt.Columns.Add(New DataColumn() With {.ColumnName = "UserId", .Caption = "User Id", .DataType = GetType(Int32)})
dt.Columns.Add(New DataColumn() With {.ColumnName = "Username", .Caption = "Username", .DataType = GetType(String)})
dt.Columns.Add(New DataColumn() With {.ColumnName = "Education", .Caption = "Education", .DataType = GetType(String)})
dt.Columns.Add(New DataColumn() With {.ColumnName = "Location", .Caption = "Location", .DataType = GetType(String)})
dt.Columns.Add(New DataColumn() With {.ColumnName = "Price", .Caption = "Price", .DataType = GetType(Decimal)})
End Sub
Public Sub InitializeDataGridView(dt As DataTable)
'set property
DataGridView1.AutoGenerateColumns = False
'remove existing columns
DataGridView1.Columns.Clear()
'add each column that exists in the DataTable to the DataGridView
'ToDo: change/set properties as desired
For Each dataColumn As DataColumn In dt.Columns
Dim dgvCol As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn() With
{
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
.DataPropertyName = dataColumn.ColumnName,
.DefaultCellStyle = If(dataColumn.DataType = GetType(Decimal), New DataGridViewCellStyle() With {.Alignment = DataGridViewContentAlignment.MiddleCenter, .Format = "N2"}, New DataGridViewCellStyle() With {.Alignment = DataGridViewContentAlignment.MiddleCenter}),
.FillWeight = 10,
.HeaderCell = New DataGridViewColumnHeaderCell() With {.Style = New DataGridViewCellStyle() With {.Alignment = DataGridViewContentAlignment.MiddleCenter}},
.HeaderText = If(Not String.IsNullOrEmpty(dataColumn.Caption), dataColumn.Caption, dataColumn.ColumnName),
.MinimumWidth = 5,
.Name = dataColumn.ColumnName,
.Width = 100
}
'add column to DataGridView
DataGridView1.Columns.Add(dgvCol)
Next
End Sub
Public Sub AddTestData(dt As DataTable)
'add rows to DataTable
dt.Rows.Add(1, "Satinder Singh", "Bsc Com Sci", "Mumbai", 25000)
dt.Rows.Add(2, "Amit Sarna", "Mstr Com Sci", "Mumbai", 21000)
dt.Rows.Add(3, "Andrea Ely", "Bsc Bio-Chemistry", "Queensland", 35000)
dt.Rows.Add(4, "Leslie Mac", "MSC", "Town-ville", 22000)
dt.Rows.Add(5, "Vaibhav Adhyapak", "MBA", "New Delhi", 27000)
End Sub
End Class
其他资源
评论
0赞
roy
4/7/2023
谢谢你的回答。从代码中,您在 datagridview 中成功转换,但价格列的打印预览 (KtReport) 结果没有格式 (N2),我在这篇文章中也附上了屏幕截图
0赞
Tu deschizi eu inchid
4/7/2023
@user18:请在 OP 中添加有关 GenerateReport 的更多信息,包括所使用的方法/属性的代码。
0赞
roy
4/8/2023
感谢您的回复。我用于生成报告库,即 Kimtoo.Reports。我检查了 github 上的信息,并使用程序语言 C 将其附加在屏幕截图中#
1赞
Tu deschizi eu inchid
4/8/2023
问题似乎与报表库未使用 DataGridView 中设置的格式有关。您必须向库开发人员提交问题,或者在不使用该库的情况下继续。
0赞
roy
4/8/2023
因此,从您的代码中,只需将其更改为对齐到中间右侧,然后报告库将支持该格式。另一个如果我使用“列价格中的另一个示例结果 dataGridView:”,我手动创建 DataGridView 列,然后我不格式化它在报告库中工作的对齐方式。
评论
.ToString("N2")