按某列求和以及绑定下拉列表
int count=0; //总和
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//以下代码绑定下拉列表
DropDownList ddl =(DropDownList)GridView1.Rows[i].Cells[13].FindControl("ddlSex");
bool sex = (bool)ds.Tables[0].Rows[i]["Sex"];
if (ddl != null)
{
if (sex)
ddl.SelectedIndex = 0;
else
ddl.SelectedIndex = 1;
}
//以下代码求和
count += int.Parse(ds.Tables[0].Rows[i]["Score"].ToString());
}
int av = count / ds.Tables[0].Rows.Count;
GridView1.FooterRow.Cells[6].Text = "平均成绩:" + av.ToString();
给Button添加客户端事件
btnDeleteAll.Attributes.Add("onclick","return confirm('你真的要删除所选项吗?')");
读取模版列中控件的值
string studentSex = ((RadioButton)GridView1.Rows[index].Cells[3].FindControl("rbmail")).Checked ? "1" : "0";
string studentSex = ((DropDownList)GridView1.Rows[index].FindControl("ddlSex")).SelectedValue;
RadioButton绑定数库库中Sex字段
<asp:RadioButton ID="rbmail" runat="server" Checked='<%# DataBinder.Eval(Container,"DataItem.Sex") %>' Enabled="False" Text="男" />
<asp:RadioButton ID="rbfemail" runat="server" Checked='<%# !(bool)DataBinder.Eval(Container,"DataItem.Sex") %>' Enabled="False" Text="女" />
删除GridView中的某行时记得再入以下代码!
if (GridView1.Page.Items.Count==1)
{
if (GridView1.PageIndex != 0)
GridView1.PageIndex--;
}