DataGrid+DropDownList绑定已没问题,如何实现在编辑这一行时,DropDownList可以停在数据库里数据所处的位置?

hornbills 2007-01-27 04:55:26
Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyDataGrid.ItemDataBound
Dim mg as new ManageDB
Dim DS as DataSet
DS=mg.GetCompanyByID(MyDataGrid.DataKeys(Cint(E.Item.ItemIndex)))

If e.Item.ItemType = ListItemType.EditItem Then
Dim myDropDownList As New DropDownList
myDropDownList = CType(e.Item.FindControl("ddlCompanyArea"), DropDownList)
myDropDownList.DataSource = BindCompanyArea()
myDropDownList.DataTextField = "Company_Area"
myDropDownList.DatavalueField = "ID"
myDropDownList.DataBind()
myDropDownList.SelectedIndex=myDropDownList.Items.IndexOf(myDropDownList.Items.FindbyValue(DS.Tables(0).ROWS(0).Item("id_company_area").ToString))


End If
End Sub

我认为问题在这一句
mg.GetCompanyByID(MyDataGrid.DataKeys(Cint(E.Item.ItemIndex)))
E.Item.ItemIndex取出值是-1,MyDataGrid.DataKeys出错

但不知道如何处理?
...全文
211 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hornbills 2007-02-07
  • 打赏
  • 举报
回复
微软提供的答案

Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyDataGrid.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
dim DRV as DataRowView=CType(e.Item.DataItem,DataRowView)
Dim CurrentValue1 As String=DRV("Product_Status")
Dim CurrentValue2 As String=DRV("Product_Trouble")

Dim item As ListItem
Dim myDropDownList As New DropDownList

myDropDownList = CType(e.Item.FindControl("ddlProductStatus"), DropDownList)
myDropDownList.DataSource = BindProductStatus()
myDropDownList.DataTextField = "Product_Status"
myDropDownList.DatavalueField = "ID"
myDropDownList.DataBind()
item=myDropDownList.Items.FindByText(CurrentValue1)
If Not item Is Nothing Then Item.Selected=True

myDropDownList = CType(e.Item.FindControl("ddlProductTrouble"), DropDownList)
myDropDownList.DataSource = BindProductTrouble()
myDropDownList.DataTextField = "Product_Trouble"
myDropDownList.DatavalueField = "ID"
myDropDownList.DataBind()
item=myDropDownList.Items.FindByText(CurrentValue2)
If Not item Is Nothing Then Item.Selected=True
'注意,当比较字符串的时候Nchar类型字段是要经过处理才行,否则更换成NvChar
End If
End Sub
wwwhang 2007-01-27
  • 打赏
  • 举报
回复
DataRowView rowitem = (DataRowView)e.Row.DataItem;
DropDownList clsname = (DropDownList)e.Row.FindControl("uclassname");
if (rowitem["ClassName"] != DBNull.Value)
{
clsname.Items.FindByText(rowitem["ClassName"].ToString()).Selected = true;
}
hy_lihuan 2007-01-27
  • 打赏
  • 举报
回复
E.Item.ItemIndex>=0
lswweb 2007-01-27
  • 打赏
  • 举报
回复
if(e.Item.ItemIndex>=0)
{
//你的执行代码
}
tianyi_hsy 2007-01-27
  • 打赏
  • 举报
回复
E.Item.ItemIndex为-1时是DataGrid的头,为0时才是第一行,
你把代码放到if(E.Item.ItemIndex != -1)中应该能解决
glacier111 2007-01-27
  • 打赏
  • 举报
回复
帮顶

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧