Solved

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

Posted on 2011-03-09
12
882 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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