如何在GridView中隐藏和显示列 [英] how to hide and show columns in gridview

查看:173
本文介绍了如何在GridView中隐藏和显示列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我如何在asp.net c#中隐藏和显示gridview中的列,
我想在由管理员批准的gridview中显示批准的数据.

我的代码是

pls some one tell that how to hide and show columns in gridview in asp.net 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.id,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.email,c.followup from Category as c inner join login_details on c.id= login_details.id where c.id=11";

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

    gvDetails.DataSource = table;
    gvDetails.DataBind();
}


if (CheckBox2.Checked == true)
{
    txtcompany.Visible = true;
    lblcompany.Visible = true;
    gvDetails.Columns["compname"].Visible = true;
}
else
{
    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.

推荐答案

错误在此行:
The error is in this line:
gvDetails.Columns["compname"].Visible = true;


并在此行中:


and in this line:

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


您需要将[和]之间的string更改为整数.
整数必须是列的索引.


You need to change the string between [ and ] into a integer.
The integer must be the index of the column.


错误在此行代码中

The error is in this line of code

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



大括号[]中必须有一个整数值.它不过是列的索引值.假设您有五个列A,B,C,D& E.它们的索引值将是0、1、2、3和. 4.如果要在此处隐藏第四列,例如 compname (即索引值= 3),则必须像这样修改代码



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.


这篇关于如何在GridView中隐藏和显示列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆