Gridview编辑状态下CheckBoxList和DropDownList数据绑定
这两天老老大在泰国,我做的POP系统需要让我增加两个功能,其中就用到了在Gridview编辑状态下控件绑定数据库的问题,查询了相关资料后终于搞定了。将主要的代码写在这里,记录一下。
主要就是一个:
e.Row.RowState == DataControlRowState.Edit ||
e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit
我觉得主要是用来判断Gridview的行的状态,之前没有加这个条件,所以一直是没有绑定到对象之类的错误提示。
主要的代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断是否是DataRow,以防止鼠标经过Header也有效果
if (e.Row.RowType == DataControlRowType.DataRow)
{
//行的状态是: 编辑状态 或者 (交替行且是编辑状态)
if (e.Row.RowState == DataControlRowState.Edit ||
e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)
)
{
//下面是绑定CheckBoxList,根据Gridview中的显示的值,更改CheckBoxList的选中状态
Label lab = (Label)e.Row.FindControl(“TxtPrinter”);
CheckBoxList chk = (CheckBoxList)e.Row.FindControl(“OperatorList”);
string txtPrinter = lab.Text;
if (txtPrinter != “”)
{
for (int i = 0; i < chk.Items.Count; i++)
{
if (txtPrinter.IndexOf(chk.Items[i].Text.ToString()) >= 0)
{
chk.Items[i].Selected = true;
}
}
}
//下面是绑定DropDownList,根据Gridview Cells中显示的内容查找数据库,如果存在相关信息就显示DropDownList,并绑定
DropDownList printroll = (DropDownList)e.Row.FindControl(“PrintRollList”);
TextBox txt_printroll = ((TextBox)e.Row.FindControl(“txtPrintroll”));
BLL.printroll bll = new BLL.printroll();
string pid = bll.GetID(txt_printroll.Text.Trim());
if (pid == “–”)
{
printroll.Visible = false;
}
else
{
DataTable dt = bll.GetChildList(Convert.ToInt32(pid));
if (dt.Rows.Count != 0)
{
printroll.DataSource = dt;
printroll.DataTextField = dt.Columns["printroll"].ToString();
printroll.DataValueField = dt.Columns["printroll"].ToString();
printroll.DataBind();printroll.Items.Insert(0, “–PrintRoll–”);
printroll.Items[0].Value = “0″;
printroll.Visible = true;
}
}
}
}}












}
}








最新评论