日期:2014-05-18  浏览次数:20484 次

gridview中Combox如何設定默認值
在gridview中有combox控件,但combox選中的值根據當前行的值來確認選擇哪一項。
C# code

protected void gv_EmpInfor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
            {
                GridViewRow drv = e.Row;
                //首級部門的代號
                string sparentcode = ((System.Data.DataRowView)(drv.DataItem)).Row.ItemArray[9].ToString();
                //首級部門的編碼
                 string sPartDeptcode = ((System.Data.DataRowView)(drv.DataItem)).Row.ItemArray[10].ToString();

                //職位表中的職位編碼
                string stechcode = ((System.Data.DataRowView)(drv.DataItem)).Row.ItemArray[8].ToString();
                //員工表中的架構職位代碼
                string sEmpTechcode = ((System.Data.DataRowView)(drv.DataItem)).Row.ItemArray[6].ToString();
                //如果員工表中架構職位代碼為空則自動綁定初始部門,子部門,職位并生成編碼。

                //首級部門控件
                Control control = e.Row.FindControl("rcb_ParentDept");
                if (control != null)
                {
                    DropDownList rcb_firstdept = control as DropDownList;
                    rcb_firstdept.DataSource = GetParentDept();
                    
                    rcb_firstdept.DataTextField = "departmentname";
                    rcb_firstdept.DataValueField = "PartDeptCode";
                    rcb_firstdept.DataBind();
                    rcb_firstdept.Items.Insert(0, new ListItem("請選擇", "-1"));
                   // rcb_firstdept.SelectedItem.Text = "集團";
                    rcb_firstdept.Attributes.Add("RowIndex", e.Row.RowIndex.ToString());
                    if (sEmpTechcode == string.Empty)
                    {
//此項是關鍵-----我設置了這項值可顯示的值還是“請選擇”
                        rcb_firstdept.SelectedItem.Value = "00002$FA";// sparentcode + "$" + sPartDeptcode;
                       // rcb_firstdept.SelectedIndex = rcb_firstdept.Items.IndexOf(new ListItem("生產部"));
                    }

                }
                //子部門控件
                Control control1 = e.Row.FindControl("rcb_SubDept");
                if (control != null)
                {
                    DropDownList rcb_subDept = control1 as DropDownList;
                    rcb_subDept.Attributes.Add("RowIndex", e.Row.RowIndex.ToString());

                }
                //職位列表控件
                Control control2 = e.Row.FindControl("rcb_Technical");
                if (control != null)
                {
                    DropDownList rcb_tchnical = control2 as DropDownList;
                    rcb_tchnical.Attributes.Add("RowIndex", e.Row.RowIndex.ToString());

                }
            }
        }
    }



//此項是關鍵-----我設置了這項值可顯示的值還是“請選擇”

------解决方案--------------------
你把默认值 外面加一个判断,如果已有选择就不让它走你的默认值了! 否则,程序每走到这,都会给那个下拉值赋成 “请选择”