VS.Net 中为ComboBox 和DropDownList 添加行...

VS.Net 中为ComboBox DropDownList 添加行

因为ASP.Net WebForm DropDownList WinForm 中的ComboBox 表现出来的形式是相同的,但我们添加的做法却是不同的,我们下面为其添加行做一个比较。

---这里的添加是 绑定数据源之后的添加

-Asp.net WebForm

Dim ds As DataSet = iobj_sql.GetRowsByQuery(Sql)

Dim dItem As ListItem = New ListItem("要添加的行", "要添加行的 Value")

FilterProjectOwerDropDownList.DataSource = ds

FilterProjectOwerDropDownList.DataTextField = "Name"

FilterProjectOwerDropDownList.DataValueField = "Value"

FilterProjectOwerDropDownList.DataBind()

FilterProjectOwerDropDownList.Items.Insert(0, dItem)

-WinForm

Dim ldtb_temp As New DataTable

ldtb_temp.Columns.Add("Name")

ldtb_temp.Columns.Add("Value")

Dim ldtb_row As DataRow

ldtb_row = ldtb_temp.NewRow

ldtb_row.Item("Name") = "Name1"

ldtb_row.Item("Value") = "Value1"

ldtb_temp.Rows.Add(ldtb_row)

ldtb_row = ldtb_temp.NewRow

ldtb_row.Item("Name") = "Name2"

ldtb_row.Item("Value") = "Value2"

ldtb_temp.Rows.Add(ldtb_row)

ComboBox1.DataSource = ldtb_temp

ComboBox1.DisplayMember = "Name"

'----上面绑定完成,下面加你想加的内容----

ldtb_row = ldtb_temp.NewRow

ldtb_row.Item("Name") = "加一个空行"

ldtb_row.Item("Value") = "加一个空行Value"

ldtb_temp.Rows.InsertAt(ldtb_row, 0)

Me.ComboBox1.SelectedIndex = 0

你可能感兴趣的:(.net,asp.net,webform,asp,WinForm)