我有一个使用自动生成列的网格视图,因为用户可以选择在查询中返回哪些列。我想要隐藏标识列。我该如何隐藏自动生成列?即使在数据绑定事件中,列计数也为零。
我想隐藏自动生成列,可以通过在GridView的AutoGeneratingColumn事件中检查每个列是否是标识列(identity),如果是,则将其IsVisible属性设置为false。
我发现了如何做到这一点。您需要使用rowdatabound事件,并在绑定行时隐藏单元格。
Protected Sub ResultGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ResultGrid.RowDataBound
e.Row.Cells(1).Visible = False
End Sub
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[1].Visible = false;
}
protected void grd_DataBound(object sender, EventArgs e)
{
try
{
HideAutoGeneratedGridViewColumn(grd, "nContractID");
}
catch (Exception ex)
{
}
}
public int getColumnIndex(GridView grd, string sColumnName)
{
return getColumnIndex(grd, sColumnName, false);
}
/// <summary>
/// Returns the columns index of the specified column based on the header text.
/// </summary>
/// <param name="grd"></param>
/// <param name="sColumnName"></param>
/// <returns></returns>
public int getColumnIndex(GridView grd, string sColumnName, bool bAutoGeneratedColumn)
{
int ReturnVal = -1;
try
{
if (grd != null)
{
if (!bAutoGeneratedColumn)
{
#region Static Columns
if (grd.Columns.Count > 0)
{
for (int x = 0; x < grd.Columns.Count; x++)
{
if (grd.Columns[x] != null)
{
if (grd.Columns[x].HeaderText.ToLower() == sColumnName.ToLower())
{
ReturnVal = x;
break;
}
}
}
}
#endregion
}
else
{
#region AutoGenerated Columns
if (grd.HeaderRow != null)
{
for (int x = 0; x < grd.HeaderRow.Cells.Count; x++)
{
if (grd.HeaderRow.Cells[x] != null)
{
if (grd.HeaderRow.Cells[x].Text.ToLower() == sColumnName.ToLower())
{
ReturnVal = x;
break;
}
}
}
}
#endregion
}
}
}
catch (Exception ex)
{
ReturnVal = - 1;
LogMessage("getColumnIndex(GridView grd, string sColumnName, bool bAutoGeneratedColumn) Error", ex.Message);
}
return ReturnVal;
}
/// <summary>
/// Returns the columns index of the specified column based on the header text.
/// </summary>
/// <param name="sColumnName"></param>
/// <param name="r"></param>
/// <returns></returns>
public int getColumnIndex(string sColumnName, GridViewRow r)
{
int ReturnVal = -1;
try
{
if (r != null)
{
if (r.Cells.Count > 0)
{
for (int x = 0; x < r.Cells.Count; x++)
{
if (r.Cells[x] != null)
{
if (((System.Web.UI.WebControls.DataControlFieldCell)(r.Cells[x])).ContainingField.HeaderText == sColumnName)
{
ReturnVal = x;
break;
}
}
}
}
}
}
catch (Exception ex)
{
ReturnVal = -1;
}
return ReturnVal;
}
public void HideAutoGeneratedGridViewColumn(GridView grd, string sColumnName)
{
HideAutoGeneratedGridViewColumn(grd, getColumnIndex(grd, sColumnName, true));
}
public void HideAutoGeneratedGridViewColumn(GridView grd, int nColumnIndex)
{
try
{
grd.HeaderRow.Cells[nColumnIndex].Visible = false;
for (int x = 0; x < grd.Rows.Count; x++)
{
grd.Rows[x].Cells[nColumnIndex].Visible = false;
}
}
catch (Exception ex)
{
LogMessage("HideAutoGeneratedGridViewColumn(GridView grd, int nColumnIndex) Error", ex.Message);
}
}
你需要它吗?最简单的方法是不在选择查询中包含它。
如果你需要它并且知道列的位置:
gridView.Columns[KnownColumnIndex].Visible = false;
这将隐藏自动生成的列标题和单元格,而不会像数据绑定那样看起来混乱。这是从这里获取的正确答案。
Protected Sub Gdvisitor_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gdvisitor.RowCreated
If (e.Row.Cells.Count > 1) Then
e.Row.Cells(1).Visible = False
End If
End Sub
我会检查列是否大于零,如果是,则使用列名称和整数引用列集合的事实将标识列设置为隐藏。