簡體   English   中英

如何從GridView中刪除列,其中DataSource是DataTable,而AutoGenerateColumns為true?

[英]How to delete column from GridView where DataSource is a DataTable and AutoGenerateColumns is true?

只是想知道C#是否可行。

我有一個GridView。 GridView的數據源是從數據庫生成的數據表。 AutoGenerateColumn為True。

因此,當我嘗試以下代碼時

gridView.Columns.RemoveAt(1); //I got 12 Columns from the DataTable

我收到以下錯誤:

異常詳細信息:System.ArgumentOutOfRangeException:索引超出范圍。 必須為非負數並且小於集合的大小。

我不想將其從DataTable中刪除。 在使用RenderControl將GridView轉換為html文本之前,我想刪除該列。 而且我不希望該列以html文本顯示。 我也嘗試這樣做:

foreach (GridViewRow row in gv.Rows){
    row.Cells[1].Visible = false;
}

但是它不會隱藏列標題。

有人知道是否可以刪除列嗎?

嘗試這個

gridView.columns.RemoveAt(1);
gridView.Databind();

試試這個:在GridView1.RowDataBound事件上

GridView1.Columns(0).Visible = False

要么

protected void bla_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false; // hides the first column
}

實際上,每次GridView渲染時,都會多次調用RowCreated。 但是有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM