下拉列表动态排序值

时间:2012-10-03 05:57:50

标签: asp.net vb.net gridview html.dropdownlistfor

我在gridview中有一个下拉列表我在这个数据库的下拉列表中选择一个数据

Adeel
Akram
Ali
Asif
Zeeshan

问题是当gridview加载例如5行用于数据插入数据时显示在下拉列表中按照这样的方式排序每行第一个元素是adeel但是我想按行排序名称例如行元素应显示为adeel,第二行值应显示为Akram,依此类推.Below是我用于加载griview进行数据插入的代码

For Each gvRow As GridViewRow In GridView1.Rows
    Dim ddlEmpCode As DropDownList = CType(gvRow.FindControl("DropDownlist1"), DropDownList)
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseConnectionString2").ConnectionString)
    Dim cmd As SqlCommand = New SqlCommand("insert into Status(m_date,emp_code) values (@m_Date,@EMp_Code)", con)
    cmd.Parameters.Clear()
    cmd.Parameters.AddWithValue("m_Date", txtdate.Text)
    cmd.Parameters.AddWithValue("Animal_Code", ddlAnimalCode.SelectedValue)
    con.Open()
    cmd.ExecuteNonQuery()
    con.Close()
    Label1.Text = "All Records are Saved Successfully"
    btnInsert.Enabled = False
Next

1 个答案:

答案 0 :(得分:0)

尝试使用以下code..try在rowdatabound事件中设置选定的下拉列表,如下所示...

protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList drpdwnList = ((DropDownList)e.Row.FindControl("DropDownlist1"));                   
                drpdwnList.SelectedIndex=e.Row.RowIndex;
            }
        }

**我已经用C#编写了这个,你需要在Vb中编写相同的文件