Click here to Skip to main content
13,095,921 members (53,813 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In my N Tier application, I have a dataset with following code:

                int acid;
                acid = Convert.ToInt32(textBoxActivityNumber.Text);
                _localds = new DataSet("CRMBCC");
                _localds = myActivity.GetAllServiceRequestsFromActivity(acid);//acid
                dataGridViewActivityServiceRequest.DataSource = _localds.Tables[0];
                //Make unvisible columns
                dataGridViewActivityServiceRequest.Columns["ActivityId"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["ServiceRequestId"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["ActivityServiceRequestId"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["Description"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["Comments"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["OperationNumber"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["ProductId"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["ProductId1"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["Active"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["Type"].Visible = false;
                dataGridViewActivityServiceRequest.Columns["Line"].Visible = false;
                //Order columns                   
                dataGridViewActivityServiceRequest.Columns["ServiceRequestId1"].DisplayIndex = 1;
                dataGridViewActivityServiceRequest.Columns["Area"].DisplayIndex = 2;
                dataGridViewActivityServiceRequest.Columns["SubArea"].DisplayIndex = 3;
                dataGridViewActivityServiceRequest.Columns["Name"].DisplayIndex = 4;
                dataGridViewActivityServiceRequest.Columns["Priority"].DisplayIndex = 5;
                dataGridViewActivityServiceRequest.Columns["Status"].DisplayIndex = 6;
            catch (Exception ex)
                MessageBox.Show(ex.Message, "Activity Service Requests",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

Now I would like to give the visible columns another more meaningfull name AND I would like to autoformat the width of the columns?
Can someone help me with this?

Kind regards!!
Posted 10-Dec-12 0:27am
Updated 10-Dec-12 0:54am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To set the auto column width:
dataGridView1.Columns["Col1"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

To give custom column headers:
dataGridView1.Columns["Col1"].HeaderText = "Custom Header";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100