Solved

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

Posted on 2011-03-09
12
878 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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 …

773 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