1没有数据的时候显示表头
#region 数据绑定
private void bind(bool a)
{
dbconnection conn = new dbconnection();
DataTable dt = new DataTable();
string sql;
sql = "SELECT GI.CName AS GCName, A.CName AS HCName, B.CName AS ACName, Matchs_TeamFact.Goals AS HGoals,Matchs_TeamFact.Losts AS AGoals, Matchs_TeamFact.HalfGoals AS HGoals_half, Matchs_TeamFact.HalfLosts AS AGoals_half, Matchs_Info.StartTime,Matchs_Info.MatchID FROM Matchs_Info INNER JOIN Games_Info GI ON Matchs_Info.GamesID = GI.GamesID INNER JOIN Matchs_TeamFact ON Matchs_Info.MatchID = Matchs_TeamFact.MatchID AND Matchs_Info.HomeTeamID = Matchs_TeamFact.TeamID INNER JOIN Teams_Info A ON Matchs_Info.HomeTeamID = A.TeamID INNER JOIN Teams_Info B ON Matchs_Info.AwayTeamID = B.TeamID Where Matchs_Info.MatchStatus=0 and Matchs_Info.flag=1 ORDER BY Matchs_Info.StartTime DESC";
dt = conn.ExecuteSelectTable(sql);
if (dt.Rows.Count != 0)
{
gv.DataSource = dt;
gv.DataBind();
}
else
{
#region 设置当没有数据的时候显示表头
dt.Rows.Add(dt.NewRow());
gv.DataSource = dt;
gv.DataBind();
int columnCount = gv.Rows[0].Cells.Count;
gv.Rows[0].Cells.Clear();
gv.Rows[0].Cells.Add(new TableCell());
gv.Rows[0].Cells[0].ColumnSpan = columnCount;
gv.Rows[0].Cells[0].Text = "没有符合条件的比赛!";
#endregion
}
}
#endregion
2.自定义列
1.建立一个模版列,在列里面添加一个labale ,id为label1
2.在它的 RowDataBound事件里面输入代码
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label)e.Row.FindControl("Label1");
if (lbl != null)
{/*
lbl.Text = string.Format("{0}:{1}({2}:{3}){4}", DataBinder.Eval(e.Row.DataItem, "HGoals"),
DataBinder.Eval(e.Row.DataItem, "AGoals"), DataBinder.Eval(e.Row.DataItem, "HGoals_half"),
DataBinder.Eval(e.Row.DataItem, "AGoals_half"), DataBinder.Eval(e.Row.DataItem, "flag"));*/
lbl.Text = string.Format("{0}:{1}({2}:{3})[待审]", DataBinder.Eval(e.Row.DataItem, "HGoals"),
DataBinder.Eval(e.Row.DataItem, "AGoals"), DataBinder.Eval(e.Row.DataItem, "HGoals_half"),
DataBinder.Eval(e.Row.DataItem, "AGoals_half"));
}
}
}
三.分页
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
bind(false);
}
四.使用按钮列
1.建立一个buttonfiled列
填写它的buttontype,属性;填好它的commandname属性test
2,把这个列转换为一个模版列
3,在页面的代码视图里面添加
<ItemTemplate>
<asp:Button ID="btn_add" runat="server" CausesValidation="false" CommandName="test"
Text="添 加" CommandArgument='<%# string.Format("{0}|{1}",Container.DataItemIndex,Eval("MatchID")) %>'/>
</ItemTemplate>
主要就是这一句CommandArgument='<%# string.Format("{0}|{1}",Container.DataItemIndex,Eval("MatchID")) %>'/>取得了它的当前列的MatchID,其中有一列这样定义的<asp:BoundField DataField="MatchID" HeaderText="[比赛ID]" />
4.在girdview里面的RowCommand事件里写代码
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "test")
{
string[] iMatch_str = e.CommandArgument.ToString().Split('|');
Response.Redirect("s_add.aspx?id=" + iMatch_str[1]);
}
}
五使用隐藏列
1.在girdview里面建立一个列可用在添加列的时候设置她的vistable属性为false,也可在页面初始化的时候动态设置 girdview1.Columns[5].Visible = false;
2.假如要取得这个隐藏列的数据应该这样
21.在girdview1设置DataKeyNames属性,值为一个数据表字段的名字handicaptype
22.建立一个隐藏列其绑定的字段为handicaptype
这个girdview如下
<asp:GridView
ID="gv" runat="server" AutoGenerateColumns="False" CellPadding="0" GridLines="None"
ShowHeader="False" DataKeyNames="handicaptype">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" />
<asp:BoundField DataField="handicaptype" Visible="False" />
</Columns>
</asp:GridView>
23 取得她的值
protected void Submit1_ServerClick(object sender, EventArgs e)
{
int rowindex;
foreach (GridViewRow gvr in gv.Rows)
{
System.Web.UI.WebControls.CheckBox cb = new CheckBox();
cb = (CheckBox)gvr.FindControl("CheckBox1");
if (cb.Checked)
{
rowindex = gvr.RowIndex; //找到gv的当前列
DataKey key = gv.DataKeys[rowindex]; //找到当前列的DataKey
string Handicaps = key.Value.ToString(); //将这个DataKey的值转换为字符串赋值给字符串,这样就得到了
handicaps.Value = handicaps.Value.ToString().Trim() + Handicaps + "|";
}
}
// Response.Write(handicaps.Value);
Session["Handicaps"] = handicaps.Value;
Response.Redirect("Handicaps_Add.aspx?id="+Request.QueryString.Get(0).Trim().ToString());
}