Hi Guys,

Have a random problem as to why my headers are not being shown properly in the form? Possibly a simple anwser but can't see the wood for the tree's at the moment and another pair of eyes is always great.

Code below:

        private void radioButton1_Click(object sender, EventArgs e)
            //Creates new versions of the Connection string and Data Set//
            con = new System.Data.OleDb.OleDbConnection();
            ds = new DataSet();

            //The actual connection to the database//
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = E:/Orders1.mdb";

            //The SQL String you need to pass into the Data Adapter to collect the information//
            string CashCustomerSQL = "SELECT * from cashCustomers";
            da = new System.Data.OleDb.OleDbDataAdapter(CashCustomerSQL, con);

            //The Data Adapater (da) is told to fill the DataSet (ds) with the information pulled from the SQL Query and call this fill "cashCustomers"//
            da.Fill(ds, "cashCustomers");

            //Opens the connection//

            //Closes The Connection//

            //Tells the dataGridView to load with the information stored in table called 'cashCustomers' in the DataSet (DS)//
            dataGridView1.DataSource = ds.Tables["cashCustomers"];

            //Change the Headers on the DataGridView//
            dataGridView1.Columns[0].HeaderText = "Account";
            dataGridView1.Columns[2].HeaderText = "Company Name";
            dataGridView1.Columns[6].HeaderText = ".Town";

            //This Code lets you decide what information from the DataSet is shown in the Grid View//
            dataGridView1.Columns["CashAccRefID"].Visible = false;
            dataGridView1.Columns["CashAccRef"].Visible = true;
            dataGridView1.Columns["CashName"].Visible = true;
            dataGridView1.Columns["CashAddress1"].Visible = false;
            dataGridView1.Columns["CashAddress2"].Visible = false;
            dataGridView1.Columns["CashAddress3"].Visible = false;
            dataGridView1.Columns["CashAddress4"].Visible = false;
            dataGridView1.Columns["CashTown"].Visible = true;
            dataGridView1.Columns["CashAccountRef_FKID"].Visible = false;

Also attached a screenshot.

Look forward to hearing from some of you.


Attachments Capture2.PNG 9.83 KB
5 Years
Discussion Span
Last Post by sundog1

Hi ddanbe,

Thanks for your reply.. that didn't work either.


Where you assign the header text, call the array by column name rather than position number.

So dataGridView1.Columns["CashAccRef"].HeadText = "Account";

You've mis-counted your array indeces. I'm making the presumption that the naming below, is also the index position they have in the array. In which case you should be using indexes 1, 2 and 7.

However, should the order change it will be wrong again. Safer to use column names :)

Votes + Comments
Great thinking!

Have you set the HeaderCell property for the DGV to true?

dataGridView1.Columns["CashAccRef"].HeaderCell.Visible = true;


He's just setting the wrong columns is all.

If you look at the screenshot, his "Company Name" header works, which is index 2. He's just mis-counted the array index for the other two columns. Should be using named indexing to avoid this issue ;)


Thanks guys,

In this instance I have taken Ketsuekiame's advice and changed the named indexing for this little job.

Thanks for the support. Great to know there are peeps like you out there for us n00bs' ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.