Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-09
12
Medium Priority
?
891 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
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

926 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