100分求gridview问题

fengyeng 2006-08-09 05:09:57
如何双击gridview的某一列然后就将这列隐藏!

感谢大家!
期待回复中..........
...全文
212 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zpengenpz 2006-08-10
  • 打赏
  • 举报
回复
顶 学习一下
wen7679 2006-08-10
  • 打赏
  • 举报
回复
关注
jc15271149 2006-08-10
  • 打赏
  • 举报
回复
孟子的不错啊
wjjdnajj 2006-08-10
  • 打赏
  • 举报
回复
孟大哥的方法很好了
wuda8 2006-08-10
  • 打赏
  • 举报
回复
zhuangyan2004 2006-08-10
  • 打赏
  • 举报
回复
关注
fengyeng 2006-08-10
  • 打赏
  • 举报
回复
谢谢大家我一会试。。。。。。
WeekZero 2006-08-09
  • 打赏
  • 举报
回复
写了个例子,test01.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test01.aspx.cs" Inherits="gridview_test01" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>

<script language="javascript">
function dbclick(N)
{
document.getElementById("HiddenField1").value = N;
document.getElementById("Button1").click();
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="80%" CellPadding="4" ForeColor="#333333"
AllowPaging="True" PageSize="12" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px"
OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" EmptyDataText="没有数据记录!!"
AllowSorting="True" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="编号" DataField="id" Visible="False" />
<asp:BoundField HeaderText="姓名" FooterText="姓名" DataField="name" SortExpression="name" />
<asp:BoundField HeaderText="身份证号" FooterText="身份证号" DataField="card" SortExpression="card" />
<asp:BoundField HeaderText="价格" FooterText="价格" DataField="price" DataFormatString="{0:¥#,##0.00}"
HtmlEncode="False" SortExpression="price" />
<asp:BoundField HeaderText="数字" FooterText="数字" DataField="price" DataFormatString="{0:0.00}"
HtmlEncode="False" SortExpression="price" />
<asp:BoundField HeaderText="建立时间" FooterText="建立时间" DataField="createdate" DataFormatString="{0:yyyy年MM月dd日 hh时mm分ss秒}"
HtmlEncode="False" SortExpression="createdate" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" Width="0px" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>


test01.aspx.cs代码为:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class gridview_test01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridViewBind();
}

private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnString1"].ConnectionString;
string SqlStr = "SELECT * FROM test01 where id<1000";
DataSet ds = new DataSet();

try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();

SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();

GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes["ondblclick"] = "dbclick('"+i.ToString()+"');";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Columns[Convert.ToInt32(HiddenField1.Value)].Visible = false;
}
}
cctaiyang 2006-08-09
  • 打赏
  • 举报
回复
也可以对dataGridView进行重绘来实现
孟子E章 2006-08-09
  • 打赏
  • 举报
回复
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load( Object sender, EventArgs e )
{
if (!IsPostBack)
{
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";

DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}
}
}

DataSet GetData( String queryString )
{
String connectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
DataSet ds = new DataSet();
try
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
adapter.Fill(ds);
}
catch (Exception ex)
{
Message.Text = "Unable to connect to the database.";
}
return ds;
}

protected void AuthorsGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("ondblclick", "hidd(" + i.ToString() + ")");
}

}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>例子</title>
<script language="javascript" type="text/javascript">
function hidd(m)
{
var o = document.getElementById("<%=AuthorsGridView.ClientID%>")
for(i=0;i<o.rows.length;i++)
{
o.rows[i].cells[m].style.display="none"
}
}
</script>
</head>
<body>
<form id="Form1" runat="server">
<asp:Label ID="Message" ForeColor="Red" runat="server" />
<br />
<asp:GridView ID="AuthorsGridView" AutoGenerateColumns="true" runat="server" OnRowDataBound="AuthorsGridView_RowDataBound">
</asp:GridView>
</form>
</body>
</html>
孟子E章 2006-08-09
  • 打赏
  • 举报
回复
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="javascript">
function hidd(m)
{
var o = document.getElementById("<%=AuthorsGridView.ClientID%>")
for(i=0;i<o.rows.length;i++)
{
o.rows[i].cells[m].style.display="none"
}
}

</script>

<script runat="server">

void Page_Load( Object sender, EventArgs e )
{
if (!IsPostBack)
{
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";

DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}

}

}

DataSet GetData( String queryString )
{
String connectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";

DataSet ds = new DataSet();

try
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
adapter.Fill(ds);

}
catch (Exception ex)
{
Message.Text = "Unable to connect to the database.";

}

return ds;

}

protected void AuthorsGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("ondblclick", "hidd(" + i.ToString() + ")");
}

}
</script>

<html>
<body>
<form id="Form1" runat="server">
<asp:Label ID="Message" ForeColor="Red" runat="server" />
<br />
<asp:GridView ID="AuthorsGridView" AutoGenerateColumns="true" runat="server" OnRowDataBound="AuthorsGridView_RowDataBound">
</asp:GridView>
</form>
</body>
</html>
WeekZero 2006-08-09
  • 打赏
  • 举报
回复
js脚本和隐藏的控件结合实现
孟子E章 2006-08-09
  • 打赏
  • 举报
回复
添加js脚本

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧