字符串数组以一个DataGridView的列绑定数组、字符串、绑定、DataGridView

2023-09-03 08:00:06 作者:别怕有我在

我设计一个WinForm其中有一个DataGridView。 我分配一个DataTable到datagridview的。

I am designing a winform which have a datagridview. I am assigning a datatable to that datagridview.

DataTable dt = new DataTable();
dt.Columns.Add("items", typeof(string[]));
dt.Columns.Add("dateSold", typeof(string));

for (int i = 0; i < 6; i++)
{
    dt.Rows.Add(dt.NewRow());
    dt.Rows[i]["items"] = new string[] { "pencil" + i, "sharpner" + i };
    dt.Rows[i]["dateSold"] = "0" + i + "/0" + i + "/0" + i;
}
dataGridView1.DataSource = dt;

列项是一个字符串数组。我想给这个阵列中的电网之一,明杆。 但是,简单地分配数据表作为在DataGridView的数据源没有帮助。

Column items is an array of strings. I want to show this array in one coloum of the grid. But by simply assigning the datatable as the datasource of the datagridview doesn't help.

推荐答案

您使用的语法是有点怪。有重载将允许您添加一个新行,并设置一个值,在同一时间。

The syntax you're using is a bit strange. There are overloads that would allow you to add a new row and set a value at the same time.

另外,你不能有一个字符串数组是一个单元一个领域。而不是使用一个数据表,你可以创建一个自定义类,以及它们的列表绑定到DataGridView。通过这种方式,你可以有一个基础字段是一个字符串数组,然后有一个公共的属性,它们格式化你的愿望,然后显示在DataGridView。

Also, you can't have a string array be a field for a cell. Instead of using a DataTable you could create a custom class, and bind a list of them to the DataGridView. This way you could have an underlying field which is an array of strings, and then have a public property that formats them as you desire, and then displays that in the DataGridView.

下面是一个如何处理这一个示例:

Here's a sample of how to approach this:

Imports System.ComponentModel

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sales As New BindingList(Of SaleTransaction)

        For i As Integer = 1 To 6
            sales.Add(New SaleTransaction(i))
        Next

        ' None of this is necessary since the DGV will automatically read the public properties.. '

        DataGridView1.AutoGenerateColumns = False

        Dim itemsColumn As New DataGridViewTextBoxColumn

        itemsColumn.HeaderText = "Items"
        itemsColumn.ValueType = GetType(String)
        itemsColumn.DataPropertyName = "Items"

        DataGridView1.Columns.Add(itemsColumn)

        Dim dateColumn As New DataGridViewTextBoxColumn

        dateColumn.HeaderText = "Date Sold"
        dateColumn.ValueType = GetType(String)
        dateColumn.DataPropertyName = "DateSold"

        DataGridView1.Columns.Add(dateColumn)

        DataGridView1.DataSource = sales

    End Sub

    Public Class SaleTransaction
        Private _items As List(Of String)

        Public ReadOnly Property Items As String
            Get
                Return String.Join(", ", _items)
            End Get
        End Property

        Public Property DateSold As Date

        Public Sub New(ByVal itemNumber As Integer)
            Me._items = New List(Of String) From {"Pencil" & itemNumber, "Sharpner" & itemNumber}
            Me.DateSold = Date.Parse(String.Format("0{0}/0{0}/0{0}", itemNumber))
        End Sub

    End Class

End Class