Solved

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

Posted on 2011-03-09
12
873 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
Comment Utility
try:-

dgUserReview.Columns[5].Visible=false;

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
Comment Utility
sorry for 5th column it should be:-

dgUserReview.Columns[4].Visible=false;

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
Comment Utility
you can also do it by column name:-

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

Open in new window


0
 

Author Comment

by:Parth48
Comment Utility
but how can i remove it ?
0
 
LVL 9

Expert Comment

by:mayank_joshi
Comment Utility
Following link provides other possible methods for doing the same:-

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

Expert Comment

by:mayank_joshi
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:Asim Nazir
Comment Utility
Remove column
0
 

Author Comment

by:Parth48
Comment Utility
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
Comment Utility
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
Comment Utility
how can i add 4 ,7 and 14 in cells ??
0
 
LVL 9

Expert Comment

by:mayank_joshi
Comment Utility
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
Comment Utility
// 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now