Question

I've got a datalist, named gvRoute, on a webpage. This is originally ordered alphabetically.

I'm looking at re-ordering this datalist based on values stored in the OurOrder array - so basically, OurOrder[x] will say that position x (let's say, 12) should be row 5 from the original datalist. This order has already been obtained from performing various calculations elsewhere in the code.

I'm having trouble getting this back into the gvRoute datalist. The code I'm currently looking at is below:

int FromHere = 0;
int TotalRows = gvRoute.Items.Count;
DataTable dt = new DataTable();
DataTable dtcache = (DataTable)Session["MyTable"];
DataRow DRTransfer = dtcache.NewRow();
for (int x = 0; x < TotalRows - 1; x++)
{
    FromHere = OurOrder[x];
    DRTransfer = dtcache.Rows[FromHere];
    dt.ImportRow(DRTransfer);
}
gvRoute.DataSourceID = "";
gvRoute.DataSource = dt;
gvRoute.DataBind();

So what's basically (trying) to happen here is that we find out various defaults (how many rows we need etc) and build some new datatables to transfer into, as well as the DRTransfer row that will be used to transfer things back and forth.

Then, for each row, we will find out which row from gvRoute is supposed to be there, put it into the DRTransfer, and then add it to the datatable dt.

Once we're done we'll try and bind the datatable dt back to the gvRoute list.

Currently I think it's managing nearly all of this fine, apart from actually putting it back into the list. The gvRoute datalist contains various fields, and it's now complaining that these fields don't exist.

EDIT - the error it's throwing is...

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'PKID_Operation'.

PKID_Operation is the first of the fields encountered in the datalist.

EDIT - the asp code of the list is...

<asp:DataList ID="gvRoute" runat="server" DataKeyField="PKID_Operation" DataSourceID="DSRoute" GridLines="Both" RepeatColumns="1">
        <ItemTemplate>
            <asp:Label ID="FKID_ContractLabel" runat="server" Text='<%# Eval("FKID_Contract") %>' Visible="False" />
            <asp:Label ID="PKID_OperationLabel" runat="server" Text='<%# Eval("PKID_Operation") %>' Visible="False" />
            <asp:CheckBox ID="chkSel" runat="server" />
            <asp:Label ID="OperationOrderLabel" runat="server" Text='<%# Eval("OperationOrder") %>' />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="L1_NameLabel" runat="server" Text='<%# Eval("L1_Name") %>' />
            &nbsp;-
            <asp:Label ID="L2_NameLabel" runat="server" Text='<%# Eval("L2_Name") %>' />
            &nbsp;-
            <asp:Label ID="L3_NameLabel" runat="server" Text='<%# Eval("L3_Name") %>' />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Operation_NameLabel" runat="server" Text='<%# Eval("Operation_Name") %>' />
            &nbsp;-
            <asp:Label ID="Team_NameLabel" runat="server" Text='<%# Eval("Team_Name") %>' />
            &nbsp;(<asp:Label ID="PostcodeLabel" runat="server" Text='<%# Eval("Postcode") %>' />
            )<asp:Label ID="EmptyLabel" runat="server" Text='<%# Eval("Empty") %>' Visible="False" />
            &nbsp;&nbsp;&nbsp;&nbsp; (<asp:Label ID="Latitude" runat="server" Text='<%# Eval("Latitude") %>' />
            ,<asp:Label ID="Longitude" runat="server" Text='<%# Eval("Longitude") %>' />
            )<asp:Label ID="GeoOrder" runat="server" Text='<%# Eval("GeoOrder") %>' Visible="False" />
            <br />
        </ItemTemplate>
    </asp:DataList>
Was it helpful?

Solution

I think the issue is dt has no columns. You need to add the columns before importing the rows. There's probably a better way to achieve this but you could try this:

int FromHere = 0;
int TotalRows = gvRoute.Items.Count;
DataTable dt = new DataTable();
DataTable dtcache = (DataTable)Session["MyTable"];
DataRow DRTransfer;

foreach (DataColumn column in dtcache.Columns)
{
    dt.Columns.Add(column.ColumnName, column.DataType);
}

for (int x = 0; x < TotalRows; x++)
{
    FromHere = OurOrder[x];
    DRTransfer = dtcache.Rows[FromHere];
    dt.ImportRow(DRTransfer);
}
gvRoute.DataSourceID = "";
gvRoute.DataSource = dt;
gvRoute.DataBind();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top