Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
In my N Tier application, I have a dataset with following code:
 
 try
            {
                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;
 
                dataGridViewActivityServiceRequest.Refresh();
 
            }
            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 1:27am
Edited 10-Dec-12 1:54am
__TR__28.9K
v2

1 solution

Rate this: bad
good
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";
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2014
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