我有一个gridview
,如下所示,我想隐藏一个名为FREQ_BAND
的列
,在按钮的第二列中单击名为btnPmtCalculation
,位于网格视图的外部。如何执行此操作?我直到此代码,但无法继续
<asp:gridview id="gvSpectrum" runat="server" headerstyle-cssclass="HomeGridHeader"
allowpaging="True" rowstyle-wrap="false" autogeneratecolumns="False" cssclass="table table-striped table-bordered"
cellpadding="4" datakeynames="FEES_CALC_FORMULA,BAND_ID,BAND,FREQ_BAND,SPECTRUM_ID" horizontalalign="Left" showfooter="true"
pagesize="10" onpageindexchanging="gvSpectrum_PageIndexChanging" onrowcommand="gvSpectrum_RowCommand">
<Columns>
<asp:BoundField DataField = "sl_num" HeaderText="SN" />
<asp:BoundField DataField = "FREQ_BAND" HeaderText="Frequency Band" />
<asp:BoundField DataField = "BW" HeaderText="Bandwidth / Quantity" />
<asp:BoundField DataField = "RANGE" HeaderText="Range" />
</Columns>
</asp:gridview>
<asp:button id="btnPmtCalculation" runat="server" text="Generate Permenant Sepctrum Invoice" onclick="btnPmtCalculation_Click">
我执行的代码如下所示
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
bool pemenant = true;
int spectrum_id = 0;
//Button btn = (Button)sender;
////Get the row that contains this button
//GridViewRow gvr = (GridViewRow)btn.NamingContainer;
foreach (GridViewRow row in gvSpectrum.Rows)
{
if (row.RowType == DataControlRowType.Header)
{
row.Cells[1].Visible = true;
}
}}
答案 0 :(得分:1)
您可以尝试以下方法:
OnRowCreated
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[columnIndex].Visible = false;
}
如果您不喜欢硬编码的索引,我建议的唯一解决方法是为GridViewColumn提供HeaderText,然后使用该HeaderText查找该列。
protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)UsersGrid.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "Email")
.SingleOrDefault()).Visible = false;
}
OnButtonClick
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[Index].Visible = false;
}
答案 1 :(得分:0)
简单,只需单击按钮即可隐藏该列。
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[i].Visible = false;
}
答案 2 :(得分:0)
您可以通过以下方式更改代码:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
bool pemenant = true;
int spectrum_id = 0;
int columnIndex = 1;
gvSpectrum.Columns[columnIndex].Visible = false;
}
'columnIndex'列号用于隐藏它
或者如果您想隐藏列标题文本,请使用此代码:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
foreach(DataControlField col in gvSpectrum.Columns)
{
if (col.HeaderText == "Email")
col.Visible = false;
}
}
答案 3 :(得分:0)
尝试一下:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[columnindex].Visible = false;
//OR
gvSpectrum.Columns["columnname"].Visible = false;
}