你應該試試這個:
在隱藏文件的代碼,
int indexOfColumn = 0; //Note : Index will start with 0 so set this value accordingly
protected void gv_gridname_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells.Count > indexOfColumn)
{
e.Row.Cells[indexOfColumn].Visible = false;
}
}
protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gv_gridname, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gv_gridname.Rows)
{
if (row.RowIndex == gv_gridname.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
}
}
}
在.aspx頁面做,
<asp:GridView ID="gv_gridname" runat="server"
OnRowDataBound="gv_gridname_RowDataBound"
OnRowDataBound="OnRowDataBound"
OnSelectedIndexChanged="OnSelectedIndexChanged">
jQuery腳本添加在你的aspx頁面:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("[id*=gv_gridname] td").hover(function() {
$("td", $(this).closest("tr")).addClass("hover_row");
}, function() {
$("td", $(this).closest("tr")).removeClass("hover_row");
});
});
</script>
Add css在你的aspx頁面:
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
td
{
cursor: pointer;
}
.hover_row
{
background-color: #FFFFBF;
}
</style>
這就隱藏了列的真正......它和我上面做的一樣。但是你無法選擇該行。這對我所做的事實際上是一個很長的路......但執行相同的任務。 –
我認爲這段代碼會隱藏你所需要的列,通過你的代碼是不可能的。 –
你有沒有試過這段代碼? –