1. gridview主键设置与获取
参考:http://topic.csdn.net/u/20090323/18/66ed67f7-f157-4150-8737-e1cd4b79bef3.html
GridView1.DataKeyNames = new string[] { "a", "b" };
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strCmd,id,catId;
id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
catId = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
}
设置方法:在设计模式下,选择“DataKeyNames" 的"...",每一行可以输入一个关键字段
2. 非gridview事件状态下获取其中某一行某一列的值.
这个时候如果不是模板列的话,一般都是Label,需要用如下才能获得
string s = this.GridView1.Rows[2].Cells[0].Controls[0]
这样写肯定点不出来的。
string s = ((HtmlInputText)this.GridView1.Rows[2].Cells[0].Controls[0]).Text
应该这样写,先强制转换,这样就可以读取了。
3.判断Gridview是否为数据行
(gvTempStaff.Rows[i].RowType == DataControlRowType.DataRow)
if (e.Row.RowType == DataControlRowType.DataRow)
4. 清清月儿 GridView 72般绝技http://blog.csdn.net/laolaowhn/archive/2007/07/05/1680124.aspx
5. 使用GridViewRow来foreach一个GridView里面的所有数据行
int departmentId = Convert.ToInt32(ddlDepartment.SelectedValue);
//for (int i = 0; i < gvNonDeptedStaff.Rows.Count - 1; i++)
foreach (GridViewRow gvr in gvNonDeptedStaff.Rows)
{
if (gvr.RowType = DataControlRowType.DataRow)
{
CheckBox chk = gvr.FindControl("chkSelct");
string staffID = gvNonDeptedStaff.DataKeys[gvr.RowIndex].Value;
Department.AddAStaffToDept(departmentId, staffID);
}
}
6. 在GridView中使用模板列添加DropDownList时如何获取当前行索引以及获取事件中的DropDownList
http://www.cnblogs.com/easypass/archive/2009/04/29/1446357.html
protected void DDLAnnex_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行
row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
int CurRow = row.RowIndex;
}
我的代码
protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;// 触发事件的 DropDownList
int status = Convert.ToInt32(ddl.SelectedValue);//触发事件的 DropDownList的值使用
GridViewRow gvr=ddl.NamingContainer as GridViewRow;// GridView 当前行
string staffID = Convert.ToString(gvStaffBasic.DataKeys[gvr.RowIndex].Value);
Staff.ModifyStatusOfStaff(staffID, status);
BindgvStaffBasic();
}