在gridview中显示下拉选择的值

时间:2013-11-15 10:47:13

标签: vb.net

gridview

中下拉列表的源代码
     <asp:TemplateField HeaderText="LocationServerName" HeaderStyle-BackColor="#C0C0C0" HeaderStyle-BorderColor="Black">
    <ItemTemplate>
    <asp:TextBox ID="txtLocationServerName" runat="server" OnTextChanged="txtLocationServerName_TextChanged"  ReadOnly="true" Text='<%# Eval("LocationServerName")%>' > </asp:TextBox>
     </ItemTemplate>                  
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProjectModifiedBy" HeaderStyle-BackColor="#C0C0C0" HeaderStyle-BorderColor="Black">
    <ItemTemplate>
     <asp:DropDownList ID="ddlProjectModifiedBy" Width="99%" runat="server" Enabled="false" 
            onselectedindexchanged="ddlProjectModifiedBy_SelectedIndexChanged" AutoPostBack="true" >
      </asp:DropDownList>
       </ItemTemplate> 
        </asp:TemplateField>

我的代码用gridview绑定下拉列表。但是,在将数据保存到数据库后,gridview没有显示gridview中下拉列表的选定值以解决此问题吗?

 Protected Sub ddlProjectModifiedBy_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim retDt = New DataTable
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Dim sqlQuery As String

    sqlQuery = "Sp_SelectEmpName"
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand(sqlQuery, con)
    Dim da = New SqlDataAdapter(cmd)
    cmd.CommandType = CommandType.StoredProcedure
    da.Fill(retDt)
    Dim dropdown = TryCast(e.Row.FindControl("ddlProjectModifiedBy"), DropDownList)

    dropDown.DataSource = retDt
    dropDown.DataValueField = "EmpId"
    dropDown.DataTextField = "EmpName"
    dropdown.DataBind()

    End If
End Sub

2 个答案:

答案 0 :(得分:0)

<ItemTemplate>
  <asp:DropDownList ID="ddlProjectModifiedBy" Width="99%" runat="server" Enabled="false" 
  onselectedindexchanged="ddlProjectModifiedBy_SelectedIndexChanged" AutoPostBack="true" >
  </asp:DropDownList>
</ItemTemplate> 

在代码中添加编辑模板

<EditTemplate>
        <asp:DropDownList ID="ddlProjectModifiedBy" Width="99%" runat="server" Enabled="false" 
  onselectedindexchanged="ddlProjectModifiedBy_SelectedIndexChanged" AutoPostBack="true" >
  </asp:DropDownList>
</EditTemplate> 

在RowDataBoound事件中添加此代码

If e.Row.RowType = DataControlRowType.DataRow AndAlso dgrd_WWWH_How.EditIndex = e.Row.RowIndex Then
  Dim ddlresp As DropDownList = CType(e.Row.FindControl("ddlResp"), DropDownList)
    'Here you get your current value from db to store any string   
     Dim lblid as Label = CType(e.Row.Fincontrol("id"),Label)
    strresp = db.getvalue(select respval from tbl where id =lblid)
   ddlresp.Items.FindByText(strresp).Selected = True
End If

答案 1 :(得分:0)

您没有将下拉列表绑定到您的数据

受保护的子ddlProjectModifiedBy_RowDataBound(ByVal sender As Object,ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
    Dim retDt = New DataTable
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Dim sqlQuery As String

    sqlQuery = "Sp_SelectEmpName"
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand(sqlQuery, con)
    Dim da = New SqlDataAdapter(cmd)
    cmd.CommandType = CommandType.StoredProcedure
    da.Fill(retDt)
    Dim dropdown = TryCast(e.Row.FindControl("ddlProjectModifiedBy"), DropDownList)

    dropDown.DataSource = retDt
    dropDown.DataValueField = "EmpId"
    dropDown.DataTextField = "EmpName"
    dropdown.DataBind()

    'I don't know the Type of your Data...
    ' next lines could be wrong just replace it with the right datatypes
    Dim dr As DataRow = CType(e.Row.DataItem, DataRowView).Row
    Try
        dropdown.SelectedValue = dr("dropDownselectedValue")
    Catch ex As Exception

    End Try
End If

End Sub

相关问题