Solved

how can i remove column from DataGrid in C#.net window application ?

Posted on 2011-03-09
12
883 Views
Last Modified: 2012-05-11
i want to know that how can i remove column from DataGrid in C#.net window application ?

please refer below code

private void Customizing_Grid()
        {
            dgUserReview.ClearSelection();
            dgUserReview.DataSource = objPortal.GetUserData();
            //Customizing the datagridview control
            dgUserReview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dgUserReview.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dgUserReview.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dgUserReview.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            dgUserReview.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgUserReview.ColumnHeadersDefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.InactiveCaptionText);
            dgUserReview.RowsDefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.InactiveCaptionText);
            dgUserReview.ColumnHeadersDefaultCellStyle.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
            dgUserReview.AutoGenerateColumns = false;
            dgUserReview.RowHeadersVisible = true;
            dgUserReview.BorderStyle = BorderStyle.FixedSingle;
            dgUserReview.CellBorderStyle = DataGridViewCellBorderStyle.RaisedHorizontal;
            dgUserReview.Rows[0].Displayed.Equals(false);
            dgUserReview.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            dgUserReview.BackgroundColor = Color.FromKnownColor(KnownColor.AppWorkspace);
            dgUserReview.Columns[0].HeaderText = "QuestionID";
            dgUserReview.Columns[1].HeaderText = "Question";
            dgUserReview.Columns[2].HeaderText = "Selected Options";
            dgUserReview.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            string[] correctoptions = General.CorrectOpt;
        }

Open in new window


now how can i remove 5the column from the above datagrid ??

0
Comment
Question by:Parth48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35080631
try:-

dgUserReview.Columns[5].Visible=false;

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35080667
sorry for 5th column it should be:-

dgUserReview.Columns[4].Visible=false;

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35080700
you can also do it by column name:-

dataGridView1.Columns["column_name"].Visible = false;

Open in new window


0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:Parth48
ID: 35080713
but how can i remove it ?
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35080722
Following link provides other possible methods for doing the same:-

http://support.microsoft.com/kb/317951
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35081013
you can also try:-

dgUserReview.Columns.Remove("Column_Name")

Open in new window


or

dgUserReview.Columns.RemoveAt(4)

Open in new window


where 4 is the column index.
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35081277
Remove column
0
 

Author Comment

by:Parth48
ID: 35081315
hi @mayank_joshi: yes u r right , it's worked ....

but now how can i add new column to it and then cells ??

can u please tell me ?
0
 

Author Comment

by:Parth48
ID: 35081335
please refer the attached snapshot ..

in the attached snapshot i add column questions , then how can i add 5 ,7 and 14 in cells ??
09-03-2011-3-27-42-PM.jpg
0
 

Author Comment

by:Parth48
ID: 35081368
how can i add 4 ,7 and 14 in cells ??
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35081456
adding and inserting row:-

this.dataGridView1.Rows.Add("five", "six", "seven", "eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");

Open in new window

0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 500 total points
ID: 35081496
// Modify the value in the first cell of the second row.
this.dataGridView1.Rows[1].Cells[0].Value = "new value";

// The previous line is equivalent to the following line.
this.dataGridView1[0, 1].Value = "new value";

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question