c#

位置:IT落伍者 >> c# >> 浏览文章

.net中gridview疑难解答


发布日期:2020年02月10日
 
.net中gridview疑难解答

在网页中制作了一个gridview其中一列是按钮列(此列已有click事件)

现在需要实现如下的功能)单击此列某个cells后能在此cells右下方出现关于点击内容的详细说明窗口(可以是窗口也可以是其他))当鼠标再点击详细说明窗口以外的区域时这个说明窗口自动消失

可直接用ToolTip属性代码如下

for (int i = ; i <= GridViewRowsCount ; i++)

{

DataRowView mydrv;

string gIntro;

if (GridViewPageIndex == )

{

mydrv = mydsTables[飞狐工作室]DefaultView;//表名

gIntro = ConvertToString(mydrv[家庭住址]);//所要处理的字段

GridViewRowsCells[]Text = SubStr(gIntro );

}

else

{

mydrv = mydsTables[飞狐工作室]DefaultView[i + ( * GridViewPageIndex)];

gIntro = ConvertToString(mydrv[家庭住址]);

GridViewRowsCells[]Text = SubStr(gIntro );

GridViewRowsCells[] ToolTip=gIntro

}

}

调用的方法

public string SubStr(string sString int nLeng)

{

if (sStringLength <= nLeng)

{

return sString;

}

string sNewStr = sStringSubstring( nLeng);

sNewStr = sNewStr + ;

return sNewStr;

}

GridView中的超链接问题

GridView中有一个字段是超链接根据该gridview中的另一个字段中内容的不同超链接指向不同的网页比如新浪网易等这样的超链接是在gridview中编辑出来然后在cs文件中写if else语句进行判断吗?

代码如下

HTML code

<%@ Page Language=C# AutoEventWireup=true EnableEventValidation=false CodeFile=GridViewaspxcs Inherits=GridView %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

<html xmlns= >

<head runat=server>

<title>无标题页</title>

</head>

<body >

<form id=form runat=server >

<div>

<asp:GridView ID=GridView runat=serverOnRowDataBound=GridView_RowDataBound >

<Columns>

<asp:BoundField DataField=学生班级 />

<asp:TemplateField>

<ItemTemplate>

<asp:HyperLink id=hl runat=server NavigateUrl=<%# getUrl(Eval(学生班级)ToString()) %> Text=<%# getName(Eval(学生班级)ToString()) %>></asp:HyperLink>

<asp:LinkButton ID=lbtn runat=server ></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</form>

</body>

</html>

C# code

using System;

using SystemData;

using SystemConfiguration;

using SystemCollections;

using SystemWeb;

using SystemWebSecurity;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemWebUIWebControlsWebParts;

using SystemWebUIHtmlControls;

using SystemDataSqlClient;

public partial class GridView : SystemWebUIPage

{

protected ICollection CreateDataSource()

{

SystemDataDataTable dt = new SystemDataDataTable();

SystemDataDataRow dr;

dtColumnsAdd(new SystemDataDataColumn(学生班级 typeof(SystemString)));

dtColumnsAdd(new SystemDataDataColumn(学生姓名 typeof(SystemString)));

dtColumnsAdd(new SystemDataDataColumn(语文 typeof(SystemDecimal)));

dtColumnsAdd(new SystemDataDataColumn(数学 typeof(SystemDecimal)));

dtColumnsAdd(new SystemDataDataColumn(英语 typeof(SystemDecimal)));

dtColumnsAdd(new SystemDataDataColumn(计算机 typeof(SystemDecimal)));

dtColumnsAdd(new SystemDataDataColumn(布尔 typeof(SystemBoolean)));

for (int i = ; i < ; i++)

{

SystemRandom rd = new SystemRandom(EnvironmentTickCount * i); ;

dr = dtNewRow();

dr[] = 班级 + iToString();

dr[] = 学生 + iToString();

dr[] = SystemMathRound(rdNextDouble() * );

dr[] = SystemMathRound(rdNextDouble() * );

dr[] = SystemMathRound(rdNextDouble() * );

dr[] = SystemMathRound(rdNextDouble() * );

dr[] = i % == ? true : false;

dtRowsAdd(dr);

}

SystemDataDataView dv = new SystemDataDataView(dt);

}

代码接上

protected string getUrl(string tempStr)

{

string url = ;

switch (tempStr)

{

case 班级:

url = ;

break;

case 班级:

url = ;

break;

case 班级:

url = ;

break;

case 班级:

url = ;

break;

default:

url = defaultaspx;

break;

}

return url;

}

protected string getName(string tempStr)

{

string name = ;

switch (tempStr)

{

case 班级:

name = 新浪;

break;

case 班级:

name = 百度;

break;

case 班级:

name = 谷歌;

break;

case 班级:

name = Tom;

break;

default:

name = defaultaspx;

break;

}

return name;

}

protected void Page_Load(object sender EventArgs e)

{

GridViewDataSource = CreateDataSource();

GridViewDataBind();

}

protected void GridView_RowDataBound(object sender GridViewRowEventArgs e)

{

if (eRowRowType == DataControlRowTypeDataRow)

{

LinkButton lbtn = eRowFindControl(lbtn) as LinkButton;

if (lbtn != null)

{

string tempStr = eRowCells[]Text;

switch (tempStr)

{

case 班级:

lbtnPostBackUrl = ;

lbtnText = 新浪;

break;

case 班级:

lbtnPostBackUrl = ;

lbtnText = 百度;

break;

case 班级:

lbtnPostBackUrl = ;

lbtnText = 谷歌;

break;

case 班级:

lbtnText = TOM;

lbtnPostBackUrl = ;

break;

default:

lbtnText = defaultaspx;

lbtnPostBackUrl = defaultaspx;

break;

}

}

}

}

}

GridView 是否有滚动条的功能?

开发的时候用到了GridView控件由于查询的数据列较多要显示很宽不知道在aspnet中有没有滚动条的功能另外能不能限定GridView的宽度不能让它自由的伸缩

可采用如下的样式

margin:auto;

width:%;

overflow:scroll;

_height:expression(thisscrollHeight>?px:auto);

               

上一篇:ado.net获取存储过程的返回值

下一篇:.net中的mapinfo开发:准备