如何将数据表的特定列绑定到 DataGridView? [英] How to Bind specific Columns of a datatable to a DataGridView?
本文介绍了如何将数据表的特定列绑定到 DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 DataTable
有从数据库中提取的三列,而我只需要将其中的两列绑定到 DataGridView
.你能帮我吗?
My DataTable
has three columns fetched from a database, while I need to bind only two columns of it to a DataGridView
. Can you please help me with it?
推荐答案
自己为 DataGridView 创建列.尝试这样的事情.
Create the columns for the DataGridView yourself. Try something like this.
DataGridView dataGridView1 = new DataGridView();
BindingSource bindingSource1 = new BindingSource();
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Field1";
dataGridView1.Columns[0].DataPropertyName = "Field1";
dataGridView1.Columns[1].Name = "Field2";
dataGridView1.Columns[1].DataPropertyName = "Field2";
bindingSource1.DataSource = GetDataTable();
dataGridView1.DataSource = bindingSource1;
这篇关于如何将数据表的特定列绑定到 DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文