简体   繁体   English

在实体框架中更改DataGridView列名

[英]Change DataGridView Column Name in Entity Framework

I have a DataTable "Items" in my SQL server Database "Procurement" 我的SQL Server数据库“采购”中有一个数据表“项目”

The DataTable has 3 columns with this schema: id (int), itemDesc (nvarchar), itemCat (int) which is bind to a DataGridView using this code: DataTable具有3个具有此架构的列:id(int),itemDesc(nvarchar),itemCat(int),使用以下代码将其绑定到DataGridView:

namespace EF_Tutorial
{
    public partial class Form1 : Form
    {

        ProcurmentsEntities ProcurmetsContext = new ProcurmentsEntities();


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            dgv_ItemList.DataSource = ProcurmetsContext.Items.ToList();
        }

    }
}

Problem is when the form loads the DataGridView shows the column name as it is in the DataTable, I tried adding the columns manually to the DGV and changed their name to that of the DataTable and the Header Text to what I want to display but the columns are generated and the one I added are left blank. 问题是当表单加载DataGridView时显示的列名称与DataTable中的名称相同,我尝试将列手动添加到DGV,并将其名称更改为DataTable的名称,并将Header Text更改为我想显示的内容,但列生成,我添加的一个留空。

Is there a work around to display the name I want ? 是否可以显示我想要的名称?

@Tima @蒂玛

Try the following: 请尝试以下操作:

dgv_ItemList.DataSource = ProcurmetsContext.Items.ToList();  
dgv_ItemList.Columns[0].HeaderText = "Item ID";  
dgv_ItemList.Columns[1].HeaderText = "Item Description";  
dgv_ItemList.Columns[2].HeaderText = "Item Category";  

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

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