如何使用datagridview查看列表中的数据表 [英] How to view the datatable in list using datagridview
本文介绍了如何使用datagridview查看列表中的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用datagridview以列表形式查看我的数据表。我读了很多。他们说使用包装但我不明白。
我尝试了什么:
此编码将在数据表中查看。
I want to view my datatable in list form using datagridview. I've read a lot. They said to use wrapper but I don't get it.
What I have tried:
This coding will view in datatable.
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
for (int j = 0; j < excelSheets.Length; j++)
{
}
foreach (String str in excelSheets)
{
dataGridView1.DataSource = dt;
}
推荐答案
您只需要设置DataSource
一次,最好使用BindingSource
。
然后网格中的所有更改都会自动反映在BindingSource中。
以下是关于它的优秀文章:详细的数据绑定教程 [ ^ ]
数据库示例:
You only need to set theDataSource
once, preferably using aBindingSource
.
Then all changes in the grid are automatically reflected in the BindingSource.
Here is an excellent article about it: A Detailed Data Binding Tutorial[^]
A database example:
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TestGrid
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void getData_Click(object sender, EventArgs e)
{
string customers = "SELECT * FROM MyUsers";
using (SqlConnection con = new SqlConnection("Data Source=MyPcName;Initial Catalog=MyDatabase;Integrated Security=True"))
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(customers, con);
da.Fill(ds, "Customers");
dataGridView.AutoGenerateColumns = true;
dataGridView.DataSource = ds;
dataGridView.DataMember = "Customers";
}
}
}
}
这篇关于如何使用datagridview查看列表中的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文