简体   繁体   English

在 C# 中将数据表的特定列绑定到 Datagridview

[英]Bind Specific Columns of Datatable to Datagridview in C#

I have a datatable that has many columns, now I have a datagridview that already have columns designed using these codes:我有一个datatable有许多列,现在我有一个datagridview那些已经使用这些代码设计的列:

frm.RationFeedsdataGridView.ColumnCount = 4;

frm.RationFeedsdataGridView.Columns[1].HeaderText = "Number";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "Name";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "Quantity";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "Percent";

Now I want to bind specific columns of my datatable to these columns of datagridview现在,我想我的具体列绑定datatable到这些列datagridview

somthing like that;类似的东西;

frm.RationFeedsdataGridView.Columns["Name"].DataPropertyName = "FeedName";

that FeedName is header of a column in datatable即FeedName是在一列中的报头datatable

what should I do?我该怎么办?

this is how I fixed my problem:这就是我解决问题的方法:

frm.RationFeedsdataGridView.AutoGenerateColumns = false;

frm.RationFeedsdataGridView.Columns[1].Name = "Name";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "Name";
frm.RationFeedsdataGridView.Columns[1].DataPropertyName = "FeedName";

frm.RationFeedsdataGridView.Columns[2].Name = "Quantity";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "Quantity";
frm.RationFeedsdataGridView.Columns[2].DataPropertyName = "Quantity";

frm.RationFeedsdataGridView.Columns[3].Name = "Percent";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "Percent";
frm.RationFeedsdataGridView.Columns[3].DataPropertyName = "Percent";

frm.RationFeedsdataGridView.DataSource = DTable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM