2012-10-03 80 views
0

我在gridview的下降列表,其中我从数据库这样下拉列表排序值动态

Adeel 
Akram 
Ali 
Asif 
Zeeshan 

问题选择在下拉列表中的数据,当例如gridview的负荷具有5行进行数据插入数据显示在下拉列表中,按每行第一个元素排序的方式排序,但我想按行排序名称示例在第一行中,元素应该显示为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 
+0

你的问题和想要的情况完全一样!?请解释更多... – Amir

+0

爵士Normaly当下拉然后显示gridivew只有第一个元素显示的每一行中,但我想说明下一个元素下一行 – user1819920

+0

爵士如果你长了它,那么请回答或者告诉我我会让更多的细节 – user1819920

回答

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#编写本,U需要编写相同在VB

+0

爵士错误显示在这一行'drpdwnList.Items.SelectedIndex = e.Row.RowIndex'为'选择的索引不是System.Web.UI.WebControls.ListItemCollection'的成员应该怎样为这个做什么? – user1819920

+0

我已编辑答案..请检查.. –