读书人

gridview的有关问题~

发布时间: 2011-12-26 23:09:58 作者: rapoo

gridview的问题~~~
gridview有两列~怎么在第一列 内容里面增加一个label 让该label读取数据库内容~ 请前辈们指点 谢谢!我新手!请说的详细些 谢谢!最好能同时给出方法和源代码 拜谢!
gridview label

[解决办法]
<asp:GridView ID= "GridView1 " runat= "server " DataSourceID= "ObjectDataSource1 " AutoGenerateColumns= "False " >
<Columns>
<asp:TemplateField HeaderText= "姓名 ">
<ItemTemplate>
<asp:Label ID= "ID " Text= ' <%# Eval( "ID ") %> ' runat= "server " Visible= "false "> </asp:Label>
<asp:Label ID= "llr " Text= ' <%# Eval( "领料人 ") %> ' runat= "server "> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
[解决办法]
模板列
[解决办法]
使用模板列可以实现
我share一个增加LinkButton的例子吧
[解决办法]
<asp:GridView ID= "gvUseBusRecord " runat= "server " CellPadding= "4 " ForeColor= "#333333 " GridLines= "None " AutoGenerateColumns= "False " CssClass= "officedoc " OnRowDataBound= "gvUseBusRecord_RowDataBound " AllowPaging= "True " AllowSorting= "True " OnPageIndexChanging= "gvUseBusRecord_PageIndexChanging " OnSorting= "gvUseBusRecord_Sorting " OnRowDeleting= "gvUseBusRecord_RowDeleting ">
<FooterStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<EmptyDataTemplate>
没有找到符合条件的数据
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField= "UserBusDate " HeaderText= "用车时间 " SortExpression= "UserBusDate " DataFormatString= "{0:yyyy年MM月dd日} " />
<asp:BoundField DataField= "RecordID " HeaderText= "记录编号 " Visible= "False " />
<asp:BoundField DataField= "PersonName " HeaderText= "用车人 " SortExpression= "PersonName " />
<asp:BoundField DataField= "StartPos " HeaderText= "起点 " SortExpression= "StartPos " />
<asp:BoundField DataField= "DestPos " HeaderText= "终点 " SortExpression= "DestPos " />
<asp:BoundField DataField= "Status " HeaderText= "状态 " SortExpression= "Status " />
<asp:BoundField DataField= "BusID " HeaderText= "派车情况 " SortExpression= "BusID "/>
<asp:CommandField DeleteText= "撤销 " ShowDeleteButton= "True " />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat= "server " id= "lbView " Text= "查看 "> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor= "#EFF3FB " />
<EditRowStyle BackColor= "#2461BF " />


<SelectedRowStyle BackColor= "#D1DDF1 " Font-Bold= "True " ForeColor= "#333333 " />
<PagerStyle BackColor= "#2461BF " ForeColor= "White " HorizontalAlign= "Center " />
<HeaderStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<AlternatingRowStyle BackColor= "White " />
</asp:GridView>
[解决办法]
protected void gvUseBusRecord_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //只处理数据行
{
//将GridView中的日期显示为短日期
DateTime dt = DateTime.Parse(e.Row.Cells[0].Text);
e.Row.Cells[0].Text = dt.ToShortDateString();
//把申请的批准状态映射为中文
DataRowView row = (DataRowView)e.Row.DataItem;
e.Row.Cells[0].ToolTip = row[ "RecordID "].ToString();
if ((int)row[ "Status "] == 0)
{
e.Row.Cells[5].Text = "未批准 ";
e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
}
else if ((int)row[ "Status "] == 1)
{
e.Row.Cells[5].Text = "已批准 ";
e.Row.Cells[5].ForeColor = System.Drawing.Color.Green;
//如果申请已经批准了,就不能撤销了
LinkButton lb = (LinkButton)e.Row.Cells[7].Controls[0];
lb.Enabled = false;
}
//设置查看LinkButton的PostUrl,这样用户点击的时候会转到申请查看的页面
LinkButton lbView = (LinkButton)e.Row.Cells[8].FindControl( "lbView ");
lbView.PostBackUrl = "ViewBusUseApply.aspx?RecordID= " + row[ "RecordID "].ToString();
//为撤销按钮添加确认对话框
LinkButton lbDelete = (LinkButton)e.Row.Cells[7].Controls[0];
lbDelete.Attributes.Add( "OnClick ", "return confirm( '您是否要撤销这个申请? '); ");
}
}

读书人网 >asp.net

热点推荐