0

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 ?

1 Answer 1

3

@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";  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.