2009-09-15 51 views
8

我有一個使用自動生成列的gridview,因爲用戶可以選擇要在查詢中返回的列。我想隱藏具有身份的列。我如何隱藏自動生成的列?即使在數據綁定事件中,列數也是零。在Gridview中隱藏自動生成的列

回答

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 
+4

+1,唯一需要注意的是您將其更改爲適當的RowTypes。例如,如果(e.Row.RowType!= DataControlRowType.Pager){e.Row.Cells [1] .Visible = false; } – 2010-01-15 09:48:51

0

我想檢查列大於零,如果是的話,我會使用列集合可以通過列名引用以及整數來將標識列設置爲隱藏的事實。

+2

自動生成的列不包含在列集合中採取了正確的答案。 – SchwartzE 2009-09-15 15:21:08

+0

在DataBound事件中做到這一點,我很確定自動生成的列將在列集合中。 – Lazarus 2009-09-15 15:30:05

0

你需要它嗎?最簡單的事情就是不要將它包含在select查詢中。

如果你需要它,知道列位置:

gridView.Columns[KnownColumnIndex].Visible = false; 
+0

我需要包含索引作爲選擇行的數據鍵。 – SchwartzE 2009-09-15 15:21:50

+0

這對我不起作用,gridView.Columns.Count對於自動生成的列爲零。 – Somebody 2012-11-01 20:47:46

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