Click here to Skip to main content
15,298,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pls some one tell that how to hide and show columns in gridview in c#,
i want to show approved data in gridview which is approved by administrator.

my code is

    string constr = @"Server=ITWLTD-DEV01;Database=parul;Integrated Security=true;";
    string query = @"select,c.username,c.password,c.compname,c.contactperson,c.designation,c.Exposurejob,c.industry,c.location,c.status,c.landtype,c.calltype,c.called,c.assigned,c.closed,c.price,c.clc,,c.followup from Category as c inner join login_details on where";

    SqlDataAdapter da = new SqlDataAdapter(query, constr);
    DataTable table = new DataTable();

    gvDetails.DataSource = table;

if (CheckBox2.Checked == true)
    txtcompany.Visible = true;
    lblcompany.Visible = true;
    gvDetails.Columns["compname"].Visible = true;
    txtcompany.Visible = false;
    lblcompany.Visible = false;
    gvDetails.Columns["compname"].Visible = false;

but it throws error
The best overloaded method match for 'System.Web.UI.WebControls.DataControlFieldCollection.this[int]' has some invalid arguments

pls some one tell the solution.
Updated 1-Nov-12 23:56pm

The error is in this line:
gvDetails.Columns["compname"].Visible = true;

and in this line:
gvDetails.Columns["compname"].Visible = false;

You need to change the string between [ and ] into a integer.
The integer must be the index of the column.
[no name] 2-Nov-12 6:00am
i dont understand the code pls tell me where i put the integer in my code...
Thomas Daniels 2-Nov-12 6:07am
Between the [ and ] chars.
For example:
gvDetails.Columns[0].Visible = true;
In this case, 0 is the column index, the first column. To set the visiblity of the second column, do this:
gvDetails.Columns[1].Visible = true;
The error is in this line of code

gvDetails.Columns["compname"].Visible = false;

You must have an integer value inside the big brackets []. It is nothing but the index value of the column. Suppose you have five columns A,B,C,D & E. Their index value will be 0,1,2,3 & 4. If you want to hide fourth column for example compname here (i.e index value = 3), then you will have to modify your code like this

gvDetails.Columns[3].Visible = false;

Hope that helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900