[英]Importing Data from file into DataGridView
I made a Windows Forms application in Visual Studio, and into initial form put one DataGridView (from toolbox). 我在Visual Studio中制作了一个Windows窗体应用程序,并将一个DataGridView(来自工具箱)放入初始窗体。 Then I made dataset and extract data from file into dataset.
然后,我制作了数据集并将数据从文件提取到数据集中。 I tried to put data from dataset into GridView but when I started the form nothing happened.
我试图将数据集中的数据放入GridView中,但是当我启动表单时,什么也没发生。 Grid was empty.
网格是空的。 What did I do wrong?
我做错了什么? Here is what I tried:
这是我尝试过的:
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.DataSource = MakeDataTable();
MakeDataTable() is method that returns DataSet (it's valid). MakeDataTable()是返回DataSet的方法(有效)。 dataGridView1 is object of class DataGridView that I got from toolbox.
dataGridView1是我从工具箱中获得的DataGridView类的对象。 The grid just remained empty, nothing happened even though there was no error in compiling.
网格只是空的,即使编译没有错误也没有发生任何事情。 Then I tried to just make DataGridView on Form1 (default form), in similar way.
然后,我尝试以类似方式在Form1(默认窗体)上制作DataGridView。 I erased DataGrid View from form Design and added line:
我从表单设计中删除了DataGrid视图并添加了以下行:
DataGridView dataGridView1 = new DataGridView();
It didn't work either, I didn't know how to put that to be visible on form. 它也不起作用,我也不知道如何将其显示在表单上。
This worked but it opened another form so I had two forms, one empty and second one with gridView properly populated with data: 这项工作有效,但是它打开了另一种形式,所以我有两种形式,一种是空的,另一种是使用正确填充数据的gridView:
Form form1 = new Form();
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.DataSource = MakeDataTable();
form1.Controls.Add(dataGridView1);
form1.ShowDialog();
What did I do wrong? 我做错了什么?
If MakeDataTable
returns a DataSet
: 如果
MakeDataTable
返回一个DataSet
:
**// Add these lines of code to your source**
DataSet ds = MakeDataTable();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
dataGridView1.AutoGenerateColumns = true;
form1.Controls.Add(dataGridView1);
form1.ShowDialog();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.