如何绑定一个数据表中的特定列到一个DataGridView? [英] How to Bind specific Columns of a datatable to a datagridView?

查看:182
本文介绍了如何绑定一个数据表中的特定列到一个DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表具有从数据库中提取的三列,而我只需要两列其绑定到datagridview.Canü请帮助我一下吧。

My Datatable has three columns fetched from a database while i need to bind only two columns of it to a datagridview.Can u pls 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屋!

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