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//
            con.Open();

            //Closes The Connection//
            con.Dispose();

            //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.

Cheers
Mark.

Recommended Answers

All 6 Replies

Did you try to put lines 27-30, behind lines 32-41?

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 :)

commented: Great thinking! +14

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' ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.