如何使用C#从gridview中删除boundfield [英] How to remove a boundfield from gridview using C#

查看:142
本文介绍了如何使用C#从gridview中删除boundfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个gridview,其中有2个固定的边界字段,1个模板字段,我希望根据我的存储过程的结果数据集添加第4个边界字段。如果数据集中有一个额外的字段,那么第4个如果再次填充gridview并且数据集不包含额外的列,则必须添加绑定字段并在回发时添加第4个边界字段...



我尝试了什么:



if(!ds.Tables [0] .columns.Contains(Country)&& gridview1.columns .contains(bfd)

{

gridview1.columns.remove(bfd);

}

I have created a gridview in which have 2 fixed bound field, 1 template field and I want a 4th bound field to be added according to the resulting dataset of my stored procedure .If there is an extra field in the dataset the 4th bound field should be added and on postback if gridview is populated again and dataset does not contain the extra column then this 4th boundfield must be removed...

What I have tried:

if(!ds.Tables[0].columns.Contains("Country") && gridview1.columns.contains(bfd)
{
gridview1.columns.remove(bfd);
}

推荐答案

使用它作为参考并构建你的



use this as reference and build yours

DataTable dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("Name");
                dt.Columns.Add("Address");
                dt.Columns.Add("NewColumn");
                dt.Rows.Add(1, "aa", "India", "NA");
                dt.Rows.Add(2, "bb", "India", "NA");
                if (dt.Columns.Count == 4)
                {
                    BoundField newcol = new BoundField() { HeaderText = "New Col", DataField = "NewColumn" };
                    gv.Columns.Add(newcol);
                }
                gv.DataSource = dt;
                gv.DataBind();


这篇关于如何使用C#从gridview中删除boundfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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