简体   繁体   中英

Change DataGridView Column Name in Entity Framework

I have a DataTable "Items" in my SQL server Database "Procurement"

The DataTable has 3 columns with this schema: id (int), itemDesc (nvarchar), itemCat (int) which is bind to a DataGridView using this code:

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.

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";  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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