0

我在gridview中有一个下拉列表,我在其中从数据库中选择下拉列表中的数据,如下所示

Adeel
Akram
Ali
Asif
Zeeshan

问题是,当gridview加载例如5行数据插入数据时,下拉显示的数据以这样的方式排序,每一行的第一个元素是adeel,但我想根据行对名称进行排序例如在第一行元素应该显示为 adeel,第二行中的值应该显示为 Akram 等等。下面是我用来加载 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
4

1 回答 1

0

尝试以下代码..尝试在 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写同样的

于 2012-10-03T06:46:14.587 回答