如何使数据表的内容出现在DGV中? [英] How do I make the content of a datatable appear in a DGV?
问题描述
我继续将旧的VB6重写为.Net作为我的.Net学习的一部分。我已经设法将CSV文件读入Datatable,当我查看'dt'时,我可以使用'datatable visualiser'查看它已正确填充,但它似乎没有填充我试图绑定的DataGridView控件它来。我花了几个小时浏览其他线程和尝试的东西,但我看不出我做错了什么。 DataGridView在App中的另一个表单上,我正在从主MDI表单中读取CSV文件(各种配置文件)。当我显示包含DataGridView的子表单时,它总是为空(它实际上似乎添加了两个数据表行,但它们的内容都是空白的。)
< pre lang =vb.net> ' 此表单称为NAMS
'
公共 dt 作为 新 DataTable
公共 ThisFilename 作为 字符串 = c:\ SitesDB.cfg
私有 Sub GetSitesDB()
Dim sr As 新 IO.StreamReader(ThisFilename)
Dim 换行符()作为 字符串 = sr.ReadLine.Split( , c)
dt.Columns。 AddRange({新 DataColumn(换行符( 0 )),新 DataColumn(换行符( 1 )),新 DataColumn(换行符( 2 )),新 DataColumn(换行符( 3 )) ,
新 DataColumn(换行符( 4 )),新 DataColumn(换行符( 5 )),新 DataColumn(换行符( 6 )),新 DataColumn(换行符(
新 DataColumn(换行符( 9 )),新 DataColumn(换行符( 10 )),新 DataColumn(换行符) ( 11 ))})
while (不 sr.EndOfStream)
newline = sr.ReadLine.Split( , c)
Dim newrow As DataRow = dt.NewRow
newrow.ItemArray = {newline( 0 ),换行符( 1 ),换行符( 2 ),换行符( 3 ),换行符( 4 ),换行符( 5 ),换行符( 6 ),换行符( 7 ),换行符( 8 ),换行符( 9 ),换行符( 10 ),换行符( 11 ) }
dt.Rows.Add(newrow)
结束 虽然
Form1.DataGridView1.DataSource = dt ' 数据可以通过悬停在'dt'上的数据可视化器看到
结束 Sub
私有 Sub ToolStripButton1_Click(发件人 As 对象,e As Eve ntArgs)句柄 ToolStripButton1.Click
Form1.Show()
结束 Sub
我尝试刷新DGV ,无济于事。我想知道在数据表绑定到DGV之后是否与Form1.Show()有关?或者我想在开始时做一个公开声明并且它无效?
但以下没有帮助
私有 Sub Form1_Load( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 MyBase .Load
' chkBit.Checked = False
' Dim cfgtext As String:cfgtext =
DataGridView1.DataSource = NAMS.dt
结束 Sub
我尝试了什么:
我已经阅读了很多线程和示例,并尝试了几种不同的东西,但似乎没有任何效果。有几种方法可以将数据输入数据表,但似乎没有任何方法将其绑定并显示在DGV中......
花了很多时间尝试,但是我显然遗漏了一些基本的过程?提供任何帮助或提示!将DataSource设置移动到Form.Shown事件中,并从语句中删除表单名称:
DataGridView1.DataSource = dt
并且不要忘记在某处执行GetSitesDB方法 - 我没有看到它被调用你的代码!
谢谢。如果我不把NAMS.dt放在Form1的代码中......
好的。让我们做一些改变!
停止在GetSitesDB中将DT值加载到DGV中。
并将Form1 Datagridview访问权限更改回私有 - 永远不要公开控件!
你有两种形式:NAMS和Form1。
NAMES包含dt变量和GetSitesDB,以及显示表单的按钮,是吗?
Form1包含DGV。
所以......更改Form1构造函数以接受DataTable,并将其保存在名为data的私有变量中。
现在更改ToolStripButton1_Click事件处理程序执行此操作:
Dim f1 as 新 Form1(数据)
f1.Show()
(这假设已经调用了GetSitesDB,如果不,也可以在这里调用)
在Form1.Shown事件中,设置DataSource:
私有 Sub Form1_Shown(se nder 作为 对象,e As EventArgs) 句柄 我。显示
DataGridView1.DataSource = data
结束 Sub
试试看看会发生什么。请告诉我!
在Form1代码中更改为以下内容...
这样的事情:
公共 类 Form1
私有 dataForDisplay As DataTable
私有 Sub 新()
InitializeComponent()
结束 Sub
Public Sub 新(数据作为 DataTable)
MyBase 。新()
InitializeComponent()
dataForDisplay = data
结束 Sub
私有 Sub Form1_Shown(发件人作为 对象,e As EventArgs)句柄 MyBase .Shown
DataGridView1.DataSource = dataForDisplay
< span class =code-keyword> Dim f1 As 新 Form1(新 DataTable)
结束 Sub
结束 类
I am continuing my rewrite of an old VB6 into .Net as part of my .Net learning. I have managed to read a CSV file into a Datatable, and I can view it has populated properly using the 'datatable visualiser' when I look at 'dt', but it doesn't seem to populate the DataGridView control I am trying to tie it to. I have spent a couple of hours browsing other threads and trying things, but I cannot see what I am doing wrong. The DataGridView is on another form in the App, and I am reading the CSV file (a config file of sorts) from the main MDI form. When I show the child form containing the DataGridView it is always empty (it actually seems to have added the two datatable rows, but their content is all blank).
' This form is called NAMS
'
Public dt As New DataTable
Public ThisFilename As String = "c:\SitesDB.cfg"
Private Sub GetSitesDB()
Dim sr As New IO.StreamReader(ThisFilename)
Dim newline() As String = sr.ReadLine.Split(","c)
dt.Columns.AddRange({New DataColumn(newline(0)), New DataColumn(newline(1)), New DataColumn(newline(2)), New DataColumn(newline(3)),
New DataColumn(newline(4)), New DataColumn(newline(5)), New DataColumn(newline(6)), New DataColumn(newline(7)), New DataColumn(newline(8)),
New DataColumn(newline(9)), New DataColumn(newline(10)), New DataColumn(newline(11))})
While (Not sr.EndOfStream)
newline = sr.ReadLine.Split(","c)
Dim newrow As DataRow = dt.NewRow
newrow.ItemArray = {newline(0), newline(1), newline(2), newline(3), newline(4), newline(5), newline(6), newline(7), newline(8), newline(9), newline(10), newline(11)}
dt.Rows.Add(newrow)
End While
Form1.DataGridView1.DataSource = dt ' data can be seen here using datatable visualiser hovering over 'dt'
End Sub
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
Form1.Show()
End Sub
I have tried refreshing the DGV, to no avail. I am wondering if its something to do with Form1.Show() being actioned after the datatable is tied to the DGV? or that I am trying to do a public statement at the beginning and it isn't valid?
But the following didn't help
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' chkBit.Checked = False
' Dim cfgtext As String : cfgtext = ""
DataGridView1.DataSource = NAMS.dt
End Sub
What I have tried:
I have read a lot of threads and examples and tried several different things, but nothing seems to work. Several approaches get the data into the datatable, but none seem to bind it and display it in the DGV......
Spent lots of hours trying, but I am obviously missing something basic in the process?? Any help or hints appareciated!
Move the DataSource setting into the Form.Shown event, and remove the form name from the statement:
DataGridView1.DataSource = dt
And don't forget to execute the GetSitesDB method somewhere - I don't see it being called in your code!
"Thanks. If I don't put NAMS.dt in the code in Form1 ..."
OK. Lets make some changes!
Stop loading the DT value into DGV in GetSitesDB.
And change the Form1 Datagridview access back to "Private" - never make controls public!
You have two forms: NAMS and Form1.
NAMES contains the dt variable, and GetSitesDB, as well as a button which displays the form, yes?
Form1 contains the DGV.
So...change the Form1 constructor to accept a DataTable, and save it in a Private variable called "data".
Now change the ToolStripButton1_Click event handler to do this:
Dim f1 as New Form1(data) f1.Show()(This assumes GetSitesDB has already been called, if not, call it here as well)
In the Form1.Shown event, set the DataSource:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown DataGridView1.DataSource = data End Sub
Try that and see what happens. Let me know!
"changed to the following in Form1 code..."
Something like this:
Public Class Form1 Private dataForDisplay As DataTable Private Sub New() InitializeComponent() End Sub Public Sub New(data As DataTable) MyBase.New() InitializeComponent() dataForDisplay = data End Sub Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown DataGridView1.DataSource = dataForDisplay Dim f1 As New Form1(New DataTable) End Sub End Class
这篇关于如何使数据表的内容出现在DGV中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!