[英]Hide DataGridView Column
我在網站上顯示“網格視圖”,但我想隱藏“ PID”列,我嘗試
dataGridView1.Columns[0].Visible = false;
但是它不起作用,這是我的數據庫連接代碼:
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SocialConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("select PID , description As Post,U_Name As Name from Post ", conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
標記:
<asp:GridView ID="GridView2" runat="server" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
Width="1000px" PageIndex="2" AllowPaging="True" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Buttonid" runat="server" CommandName='<%# Bind("PID") %>' Text="Rate" OnClick="Button_rate_click"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Link" CommandName="Reply" HeaderText="Reply" Text="Reply" />
</Columns>
</asp:GridView>
您可以嘗試從onRowCreated
事件中隱藏該列,因為它是自動生成的列。
將onRowCreated
事件添加到您的GridView標記中:
<asp:GridView OnRowCreated="gridView_OnRowCreated" ID="GridView2"
runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
Width="1000px" PageIndex="2" AllowPaging="True" >
然后在代碼后面添加方法:
protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
{
//You want to remove third column (column with index 2)
if(e.Row.Cells.Count > 2)
e.Row.Cells[2].Visible = false; // hides the first column
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.