GridView小应用随记(1)

获取某行中某单元格数据的方法

string Emp_Sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();

获取页脚模板中控件数据的方法

TextBox empRealName = GridView1.FooterRow.FindControl("txtRealName") as TextBox;

RowCommand应用

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SaveNBAnswer")
{
int index = Convert.ToInt32(e.CommandArgument);//get Row index
DataKey datakey = GridView1.DataKeys[index];
string ClarityID = datakey["ID"].ToString();
string NBAnswer = ((TextBox)GridView1.Rows[index].FindControl("txtNBAnswer")).Text;
string NBAnswerDate = DateTime.Now.ToString();
string sqlStr = "insert into ClarityAnswer(ClarityID,Answer,AnswerPersonalID,AnswerDate) values('" + ClarityID + "','" + NBAnswer + "','" + PersonalID + "','" + NBAnswerDate + "')";
if (OperateDB.ExecCmd(sqlStr))
{
// script Manager.RegisterStartup script (this, this.GetType(), "s1", "window.open('保存chenggong!');", true);
}
else
{
script Manager.RegisterStartup script (this, this.GetType(), "s1", "window.open('保存失败!');", true);
}
GridAllBind();
return;
}

}

获取行中某项数据

DataRowView mydrv = myds.Tables[0].DefaultView[i];
string score = Convert.ToString(mydrv["EmpSalary"]);

string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "EmpSex"));
if (lbl == "女")
{
e.Row.Cells[3].ForeColor = Color.Fuchsia;
}

加载

//if (ds.Tables[0].Rows.Count == 0)
//{
// ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
// gridecyl.DataSource = ds;
// gridecyl.DataBind();
// int columnCount = gridecyl.Rows[0].Cells.Count;
// gridecyl.Rows[0].Cells.Clear();
// gridecyl.Rows[0].Cells.Add(new TableCell());
// gridecyl.Rows[0].Cells[0].ColumnSpan = columnCount;
// gridecyl.Rows[0].Cells[0].Text = "暂时没有文件";

//}
//else
//{
gridecyl.DataSource = ds;
gridecyl.DataKeyNames = new String[] { "FileID" };
gridecyl.DataBind();
//}

加载二

if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gridjspcxx.DataSource = ds;
gridjspcxx.DataBind();
int columnCount = gridjspcxx.Rows[0].Cells.Count;
for (int n = 0; n < columnCount; n++)
{
gridjspcxx.Rows[0].Cells[n].Text = "";
}

}
else
{
gridjspcxx.DataSource = ds;
gridjspcxx.DataKeyNames = new String[] { "ID" };
gridjspcxx.DataBind();
}

你可能感兴趣的:(GridView)