您的位置:首页 > 数据库

gridview问题解答 :按钮的Enalbe状态 / 首页\尾页\上一页等 / 显示短时间,年\月\日 / 数据库存是1和0,显示为男和女等!

2006-12-19 13:53 796 查看
用一个GridView显示的数据表,表中一些行可以删除,另外的行不能删除。

解决:

1 数据源: sqldatasource在获取数据时,计算数据是否可以删除。这里我使用的是一个sql的function。下面是我的selectcommand:

SELECT [ContactID], [LastName], [FirstName], [EnglishName], [WorkPhone], [MobilePhone], [Email], [Address], [City], [Province], dbo.CanDelContact([ContactID]) as CanDel FROM [ContactInfo]

2 GridView中设置数据模板列的Enable属性:

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete" Enabled='<%# System.Convert.ToInt32(Eval("CanDel")) > 0 %>' />
</ItemTemplate>
</asp:TemplateField>

<%# System.Convert.ToInt32(Eval("CanDel")) > 0 %> 是在数据绑定时自动计算的。<%#表示使用数据绑顶。你可以在<%%>中间使用C#的语句进行计算。

要注意的是:GridView的asp:CommandField是不支持数据绑定的,我试过

<asp:CommandField ShowDeleteButton=<%# System.Convert.ToInt32(Eval("CanDel")) > 0 %> ButtonType =Button />

系统便以出错,因为CommandField不支持数据绑定。

所以只好增加一个模板列来解决这个问题。只要设置CommandName="Delete" ,就可以了。

******************************************************************

<%# PrintIsOpen(Convert.ToBoolean(DateBinder.Eval(Container.DataItem,"isopen")))%>
cs代码是这样的:
public string PrintIsOpen(bool bIsOpen)
{
string strHtml="";
if(bIsOpen)
{
strHtml="<img src=1>";
}
else
{
strHtml="<img src=2>";
}
return strhHtml;
}

或者说是这样的,如果我没看错你的这个判断是布尔值。
<%# Convert.ToBoolean(DateBinder.Eval(Container.DataItem,"isopen"))=true ? "img":"<img>"%>

首页\尾页\上一页\下一页\当前第几页\总共几页\跳转到第几页(下拉框)

protected void lnkPre_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex > 0)
{
this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
this.GridView1.DataBind();
}
}
protected void lnkNext_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex < this.GridView1.PageCount)
{
this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
this.GridView1.DataBind();
}

}
protected void lnkLast_Click(object sender, EventArgs e)
{

this.GridView1.PageIndex = this.GridView1.PageCount;
this.GridView1.DataBind();
}
protected void lnkFirst_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = 0;
this.GridView1.DataBind();

}

protected void GridView1_DataBound(object sender, EventArgs e)
{
int i = this.GridView1.PageIndex + 1;
labTotal.Text = "第" + i.ToString() + "页/" + "共" + this.GridView1.PageCount.ToString() + "页";
}

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Delete" OnClientClick="return confirm('真的删除吗');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Button delButton =(Button)row.Cells[0].Controls[0];
delButton.Attributes.Add("onclick", "return confirm('你确认删除吗?');");
}
}

gridView在显示时间时怎么样显示为短时间,即只显示年\月\日
DataFormatString="{0:d}"
DataFormatString="{0:yyyy/MM/dd}"
HtmlEncode="False"
GridView中怎么对一个字符型列进行格式化?
<%# DataBinder.Eval(Container.DataItem,"Field").ToString().Substring(0,5)%>
<asp:HyperLinkColumn HeaderText="sdf" DataNavigateUrlField="id" DataNavigateUrlFormatString="javascript:alert({0})" DataTextField="id" >
HtmlEncode="false"

我想在GRIDVIEW的第一列加个编号 编号规则就是 1、2、3、4、5。。。。。。请问怎么做?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = e.Row.DataItemIndex.ToString();
}
}

GridView中有个列是男1 女0,数据库存是1和0,怎么显示为男和女呢?
用模板列:
<asp:TemplateColumn HeaderText="性别">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Sex").ToString()="1"?"男":"女" %>' ID="Label1">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐