高分求解一条非常简单的C#语句 100分!!!!
public static int LoadComboFromDB(OleDbConnection connection,object ctlDropDownList, int nSGroupID, int nRefType, string nCurValue){
DropDownList ddl = (DropDownList)ctlDropDownList;
ddl.Items.Clear();
string lpszRefID = (nRefType == (int)RefType.FRT_NormalStatus) ? "StatusID" : "SysResID";
string strSQL = string.Format(" select {0}, statusName from SysStatusDict where SGroupID={1} and DispOrder>0 order by DispOrder ", lpszRefID, nSGroupID);
using(DataSet ds = DataAccess.ExecuteDataset(strSQL,connection))//读出数据
{
int nDx =0;
for( int i =0 ; i < ds.Tables[0].Rows.Count ;i++)
{
if(ds.Tables[0].Rows[i].ItemArray[0].ToString() == nCurValue)
{
nDx = i ;break;
}
}
ddl.DataSource = ds;
ddl.DataTextField = "statusName";
ddl.DataValueField = lpszRefID;
try
{
ddl.SelectedIndex = nDx;
}
catch
{
}
return ds.Tables[0].Rows.Count;
}
}
我要使下拉框的初始值为空 应该怎么做? 思路2种:1
1。在绑定前给ds(也就是数据源)前面加一条空记录,然后在绑定?
2. 不用绑定 直接用它的一些属性来赋值。也就是像 .SelectedItem之类的
我只知道思路,求大家把完整的程序帮我写下好吗? 2种我都要 --------------------编程问答-------------------- 明明是20分 --------------------编程问答-------------------- 方法1:可以考虑给ds增加一行
DataRow dr=ds.Tables[0].NewRow();
dr["statusName"]=" ";
dr["lpszRefID"]="";
ds.Tables[0].Rows.Add(dr);
方法2:绑定后再插入一项
ddl.DataSource = ds;
ddl.DataTextField = "statusName";
ddl.DataValueField = lpszRefID;
ddl.Databind()
ddl.Items.Insert(0,new ListItem(" "," "));
补充:.NET技术 , C#