如何在运行时使用名称的变量创建多个DataGridView [英] How can you create multiple DataGridViews at runtime using a variable for the name

查看:164
本文介绍了如何在运行时使用名称的变量创建多个DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找2天的答案,但到目前为止找不到一个有效的。这是我想要做的:

I have been looking for 2 days for an answer but so far can not find one that works. This is what I am trying to do:

我已经使用VB.NET
将excel工作表列表列入数组中现在我要创建一个DataGridview对于每个工作表,但我不知道如何使用表单名称作为变量,从数组创建DataGridView的。

I have read a list of excel worksheets into an array using VB.NET Now I want to create a DataGridview for each sheet but I can not figure out how use the sheet name, as a variable, from the array to create the DataGridView's.

我已经尝试了以下内容: p>

I have tried the following:

Dim dgvName as String = array(0)
Dim dgvName As New DatGridView

For x = 0 to array.length -1
   Dim array(x) As New DataGridView
Next

这两个都不行。

任何人都可以解释我该怎么做?

Can anyone explain how I can do this?

提前感谢。 p>

Thanks in advance.

推荐答案

我不会创建多个DataGridViews,因为它太重了。很可能您会在当时看到一张表,您可以通过简单地更改其 DataSource 来实现单个网格视图。

I wouldn't create multiple DataGridViews because it is too heavy. Most likely you would look at one sheet at the time and you can do it in single grid view by simply changing its DataSource.

这是你通常需要做的事情

Here is what you generally need to do


  1. 使用Microsoft.Ace.OleDb提供程序连接到Excel(连接) li>
  2. 使用 OleDbDataAdapter 填充 DataSet 。 #1中提供商的优点是可以像任何数据库表一样使用Excel,即从sheet_name

  3. 中选择*一次您填写 DataSet ,其中每个 DataTable 将表示您的工作簿中的工作表,您分配 gridView dataData.Tables(0)

  4. 创建逻辑,用户点击某些东西,并在 dataSet.Tables(index) 。这样就可以有一个网格。

  1. Use Microsoft.Ace.OleDb provider to connect to Excel (connection)
  2. Use OleDbDataAdapter to fill DataSet. The beauty of the provider in #1 is that you can work with Excel like any DB table, i.e. Select * from sheet_name
  3. Once you fill DataSet, where each DataTable will represent a sheet in your workbook, you assign gridView.DataSource = dataSet.Tables(0)
  4. Create logic, where user clicks something and changes index in dataSet.Tables(index). This way you can have one grid.

您可以轻松地研究列表中的每个项目。这里有很多资源。

You can easily research each of the items on the list. There are plenty of resources on this.

这篇关于如何在运行时使用名称的变量创建多个DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆