error message: Markus,The IListSource does not contain a data source named ''''Färger''''
The IListSource does not contain a data source named 'Färger'
<%@Page Language = "VB"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<script runat="server">
dim ds as DataSet
dim blnSet as Boolean = false
sub Page_Load(obj as object, e as EventArgs)
ds = CreateDataset
blnSet = True
if not Page.IsPostBack then
BindGrid
end if
end sub
----------------------------------------------------
sub BindGrid()
dgColors.DataSource = ds
dgColors.DataMember = "Färger"
----> DataBind() <----
end sub
----------------------------------------------------
sub ChangeColor(obj as object, e as DataGridItemEventArgs)
dim intIndex as Integer = e.Item.ItemIndex
if blnSet then
if intIndex > 0 then
dgColors.Items(intIndex - 1).BackColor = Drawing.Color.FromName(ds.Tables("Färger").Rows(intIndex - 1)("Färg"))
dgColors.Items(intIndex - 1).ForeColor = Drawing.Color.FromName(ds.Tables("Färger").Rows(6 - intIndex)("Färg"))
end if
end if
end sub
sub dgColors_Edit(obj as object, e as DataGridCommandEventArgs)
dgColors.EditItemIndex = e.Item.ItemIndex
BindGrid
end sub
sub dgColors_Cancel(obj as object, e as DataGridCommandEventArgs)
dgColors.EditItemIndex = -1
BindGrid()
end sub
sub dgColors_Update(obj as object, e as DataGridCommandEventArgs)
dim strColor as String = Ctype(e.Item.Cells(1).Controls(0), TextBox).Text
ds.Tables("Färger").Rows(e.Item.ItemIndex)("Färg") = strColor
ViewState("Färger")(e.Item.ItemIndex) = strColor
dgColors.EditItemIndex = -1
BindGrid
end sub
function CreateDataSet as DataSet
'skapa en färgvektor
dim i as Integer
dim arrColors() as String
if ViewState("Färger") is nothing then
arrColors = new String(6) _
{"red", "orange", "yello", "blue", "green", "indigo", "violet"}
ViewState("Färger") = arrColors
else
arrColors = ViewState("Färger")
end if
'skapa en tom datamängd
ds = new DataSet("MinDataMängd")
'skapa ny tabell och kolumner
dim dTable as New DataTable("Färger")
dTable.Columns.Add("Färg", GetType(String))
dTable.Columns.Add("ID", GetType(Int32))
'lägg till tabell
ds.Tables.Add("dTable")
'lägg till rader
for i = 0 to 6
dim dr as DataRow = dTable.NewRow()
dr(0) = arrColors(i).ToString
dr(1) = i
dTable.Rows.Add(dr)
next
return ds
end function
</script>
<html>
<body>
<form runat="server">
<asp:DataGrid ID="dgColors" Runat="server"
AutoGenerateColumns="False"
Width="200"
OnEditCommand="dgColors_Edit"
OnCancelCommand="dgColors_Cancel"
OnUpdateCommand="dgColors_Update"
OnItemCreated="ChangeColor">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" Runat="server" Text='<%#Container.DataItem("ID")%>'/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="färg" HeaderText="Färg" />
<asp:EditCommandColumn HeaderText="Ändra" EditText="Redigera" UpdateText="Uppdatera" CancelText="Avbryt" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>Sv: The IListSource does not contain a data source named Färger
Följande
'lägg till tabell
ds.Tables.Add("dTable")
skall ändras till
'lägg till tabell
ds.Tables.Add(dTable)
då <b>dTable</b> är ditt <b>DataTable</b> objekt och inte namnet på tabellen.
//Andreas