我有一個使用自動生成列的gridview,因爲用戶可以選擇要在查詢中返回的列。我想隱藏具有身份的列。我如何隱藏自動生成的列?即使在數據綁定事件中,列數也是零。在Gridview中隱藏自動生成的列
8
A
回答
14
我發現瞭如何做到這一點。您需要使用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
0
0
1
3210
1
我已經與周圍的以下問題砍死。我編寫了幫助函數來給我正確的列索引,然後隱藏所需的列。一旦輔助函數就緒後,您只需從gridview_databound函數調用一個班輪。
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);
}
}
0
這將隱藏自動生成的列標題和單元格沒有尋找它搞砸像數據綁定就行了。這是從here
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
相關問題
- 1. ASP.NET:在gridview中隱藏列
- 2. 隱藏自動生成的CTest目標
- 3. gridview隱藏列
- 4. 隱藏GridView ID列
- 5. 在jquery中隱藏自動完成
- 6. 什麼時候在gridview中自動生成的列被添加?
- 7. 如何在提交回前自動生成隱藏的輸入
- 8. 使用ItemTemplate隱藏GridView列
- 9. 隱藏GridView的列在asp.net 2.0
- 10. 獲取Gridview中隱藏列的值
- 11. 如何隱藏GridView中的編輯列?
- 12. 在GridView中隱藏UserId
- 13. 在gridview中隱藏空行
- 14. 防止自動生成的GridView列中的HTML編碼
- 15. 如何隱藏動態生成的div
- 16. 設置gridview列與自動生成列的位置
- 17. 如何獲取隱藏或自動生成的內容的值?
- 18. 如何隱藏在GridView的
- 19. 隱藏Gridview
- 20. 如何在AutoGenerateColumns =「true」時隱藏列GridView?
- 21. JQuery UI自動完成 - 在hiting後隱藏列表輸入
- 22. 如何隱藏gridview列按列名
- 23. 可編輯的GridView與自動生成的列
- 24. 如何定製GridView的自動生成列的樣式?
- 25. 如何在NativeScript項目中通過TypeScript隱藏自動生成的文件
- 26. 動態GridView生成
- 27. 如何隱藏GridView的列時,數據在GridView控件
- 28. Gridview顯示和隱藏特定的列
- 29. 從.aspx文件自動生成代碼隱藏文件
- 30. JQuery自動完成隱藏字段
+1,唯一需要注意的是您將其更改爲適當的RowTypes。例如,如果(e.Row.RowType!= DataControlRowType.Pager){e.Row.Cells [1] .Visible = false; } – 2010-01-15 09:48:51