需求如下:
我要往一个dropdownlist里面添一个二维数组,第一列是DataTextField,第二列是DataValueField。
如:设备编号、ei_ID;
设备名称、ei_Name;
规格型号、ei_Model;
请问这个代码怎么写?小弟是新手,请写出详细代码,谢谢了!
我要往一个dropdownlist里面添一个二维数组,第一列是DataTextField,第二列是DataValueField。
如:设备编号、ei_ID;
设备名称、ei_Name;
规格型号、ei_Model;
请问这个代码怎么写?小弟是新手,请写出详细代码,谢谢了!
DropDownList的DataValueField = "ei_id"
DropDownList的DataTextField = "ei_Text"
我最开始的时候写了一个例子,
DataSet ds = new DataSet();
DataTable dt=new DataTable();
dt.Columns.Add("text");
dt.Columns.Add("value");
DataRow dr = dt.NewRow();
dr["text"] = "设备编号";
dr["value"] = "ei_ID";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
ddlStaticParam1.DataSource = ds.Tables[0].DefaultView;
ddlStaticParam1.DataTextField = "text";
ddlStaticParam1.DataValueField = "value";
ddlStaticParam1.DataBind();
这个只能添加一项,所以请教如何用二维数组来实现需求!
如何用ListItem能不能写出详细代码呢?
dr["text"] = "设备编号" + "设备型号"; 或者用foreach(DataRow fDataRow in this.DataSet.Tables[0].Rows)
{
if (...)
ddlStaticParam1.Items.Add(new ListItem((string) fDataRow ["Text"] + "设备型号", (string) fDataRow ["Value"]));
}
dropdownlist添加这个类
{
ArrayList arrlist1 = new ArrayList();
arrlist1.Add("设备编号");
arrlist1.Add("设备名称");
arrlist1.Add("规格型号");
arrlist1.Add("是否数控");
arrlist1.Add("结构用途");
arrlist1.Add("主轴转速");
arrlist1.Add("轴数");
arrlist1.Add("整机功率");
arrlist1.Add("最大承载重量");
arrlist1.Add("最大加工直径");
arrlist1.Add("最大加工行程");
arrlist1.Add("加工精度");
arrlist1.Add("供应商");
arrlist1.Add("制造商");
arrlist1.Add("购置价值");
arrlist1.Add("入厂时间");
arrlist1.Add("保修期至");
arrlist1.Add("维修周期");
this.ddlStaticParam1.DataSource = arrlist1;
this.ddlStaticParam1.DataBind();
this.ddlStaticParam2.DataSource = arrlist1;
this.ddlStaticParam2.DataBind();
this.ddlStaticParam3.DataSource = arrlist1;
this.ddlStaticParam3.DataBind();
this.ddlStaticParam4.DataSource = arrlist1;
this.ddlStaticParam4.DataBind();
this.ddlStaticParam5.DataSource = arrlist1;
this.ddlStaticParam5.DataBind();
this.ddlStaticParam6.DataSource = arrlist1;
this.ddlStaticParam6.DataBind();
this.ddlStaticParam7.DataSource = arrlist1;
this.ddlStaticParam7.DataBind();
this.ddlStaticParam8.DataSource = arrlist1;
this.ddlStaticParam8.DataBind();
}
我主要想模仿这个来写,这个是写一维数组的,我现在要写的是二维数组;
问题还是没解决!
fDictionary.Add(1, "设备名称");
fDictionary.Add(2, "规格型号"); DropDownList fDropDownList = new DropDownList();
fDropDownList.DataValueField = "Key";
fDropDownList.DataTextField = "Value";
fDropDownList.DataSource = fDictionary;
fDropDownList.DataBind(); fDropDownList.SelectedIndex = 0;
string s = fDropDownList.SelectedItem.Value;
string s1 = fDropDownList.SelectedItem.Text;
{
private int ei_ID;
private string ei_Name;
private string ei_Model;
public myclass(int id, string name, string model)
{
this.ei_ID = id;
this .ei_Name =name;
this.ei_Model = model; }
public int ID
{
get { return ei_ID; }
set { ei_ID = value; }
}
public string Name
{
get { return ei_Name; }
set { ei_Name = value; }
} public string Model
{
get { return ei_Model; }
set { ei_Model = value; }
} public override string ToString()
{
return this.ei_Name;
}
}//这里是一个comboBox的例子
public partial class Form2 : Form
{
private myclass mycmb;
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
addcmbbox();
}
protected void addcmbbox()
{
for (int i = 0; i < 10; i++)
{
mycmb = new myclass(i, "名称", "型号");
comboBox1.Items.Add(mycmb);
}
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
myclass mycb = comboBox1.SelectedItem as myclass;
if (mycb != null)
{
MessageBox.Show(string.Format("id:{0} name:{1} model:{2}", mycb.ID, mycb.Name, mycb.Model));
}
} }
{
private int ei_ID;
private string ei_Name;
private string ei_Model;
public myclass(int id, string name, string model)
{
this.ei_ID = id;
this .ei_Name =name;
this.ei_Model = model; }
public int ID
{
get { return ei_ID; }
set { ei_ID = value; }
}
public string Name
{
get { return ei_Name; }
set { ei_Name = value; }
} public string Model
{
get { return ei_Model; }
set { ei_Model = value; }
} public override string ToString()
{
return this.ei_Name;
}
} //这里是一个comboBox的例子
public partial class Form2 : Form
{
private myclass mycmb;
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
addcmbbox();
}
protected void addcmbbox()
{
for (int i = 0; i < 10; i++) //这里你可用你的Tabla循环添加 ei_ID,ei_Name,ei_Model 三个字段
{
mycmb = new myclass(i, "名称", "型号");
comboBox1.Items.Add(mycmb);
}
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
myclass mycb = comboBox1.SelectedItem as myclass;
if (mycb != null)
{
MessageBox.Show(string.Format("id:{0} name:{1} model:{2}", mycb.ID, mycb.Name, mycb.Model));
}
} }
dt.columns.add("0", Typeof(System.string));
dt.columns.add("1", Typeof(System.string));
DataRow row;
for(int i=0; i< aa.length; i++)
{
row = dt.newrow();
row["0"] = aa[i,0];
row["1"] = aa[i,1];
dt.row.add(row);
}
DropDownList1.DataSource=dt;
DropDownList1.DataTextField="0";
DropDownList1.DataValueField="1";
DropDownList1.DataBind(); 使用item.insert()方法添加:
for(int j=0;j<aa.GetLength(0);j++)
{
ddl.Items.Add(new ListItem(aa[j,0],aa[j,1]));
}
参考
DataTable DT = new DataTable();
if (gBaseMod.TakeOutData_List(UserID, ref DT, ref DealMess) < 0) //取出数据或者用你定义好的表
{
Response.Write("<script>alert('检索数据失败! "+ DealMess +"');</script>");
return;
}
ListItem UListItem = new ListItem();
UListItem.Text = "";
UListItem.Value = "";
this.list_scdw.Items.Add(UListItem);
foreach (DataRow row in DT.Rows)
{
string text = row["BMMC"].ToString();
string value = row["BMDM"].ToString();
UListItem = new ListItem();
UListItem.Text = text;
UListItem.Value = value;
this.list_scdw.Items.Add(UListItem);
}