我有一个gridview我用来显示我的MS-SQL数据。我使用autogenerateselectbutton属性来选择每一行,以便我可以显示另一个Asp.net面板。但现在我不想在Gridview上显示“选择”列。如何隐藏它,但仍然可以选择Gridview的行。
我试图隐藏它:
gv_gridname.Columns[0].Visible = false;
我在.aspx页面上添加了Autogenerateselectbutton:
autogenerateselectbutton="true"
但它失败了,因为它没有将“Select”列计为GridView中的一列。 那么如何隐藏它并能够用它来选择我的行。请帮忙
提前致谢。
答案 0 :(得分:0)
你应该尝试这个:
在您的代码隐藏文件中,
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>
在您的aspx页面中添加css:
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
td
{
cursor: pointer;
}
.hover_row
{
background-color: #FFFFBF;
}
</style>