views:

51

answers:

1

I can't seem to bind a single GridView's row to a DetailsView properly. Currently I have this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace WebApp
{
    public partial class CrudGrid : System.Web.UI.UserControl
    {
        public const string EditCommand = "EditDialog";

        public object DataSource
        {
            get { return Grid.DataSource; }
            set { Grid.DataSource = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Grid_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch(e.CommandName)
            {
                case EditCommand:
                {
                    int index;

                    if(!int.TryParse(e.CommandArgument.ToString(), out index))
                        throw new ArgumentException();

                    TableCellCollection cells = Grid.Rows[index].Cells;

                    Details.DataSource = new object[] { new DataSourceProvider(cells[2].Text, cells[3].Text, cells[4].Text) };
                    Details.DataBind();

                    DetailsPanel.Update();
                    break;
                }
            }
        }

        protected void Grid_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        private class DataSourceProvider
        {
            public string Cell1 { get; set; }
            public string Cell2 { get; set; }
            public string Cell3 { get; set; }

            public DataSourceProvider(string cell1, string cell2, string cell3)
            {
                Cell1 = cell1;
                Cell2 = cell2;
                Cell3 = cell3;
            }
        }
    }
}

and client-side:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CrudGrid.ascx.cs" Inherits="Firelight.WebApp.WebControls.CrudGrid" %>

<asp:UpdatePanel ID="GridPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:ImageButton ID="GridInsert" ImageUrl="/images/crud/insert.png" runat="server" />

        <asp:GridView ID="Grid" GridLines="None" AllowPaging="true" PageSize="15" runat="server"
            OnRowCommand="Grid_OnRowCommand"
            OnRowDeleting="Grid_OnRowDeleting"
        >
            <Columns>
                <asp:ButtonField CommandName="EditDialog" ButtonType="Image" ImageUrl="/images/crud/edit.png" HeaderImageUrl="/images/crud/edit.png" />
                <asp:ButtonField CommandName="Delete" ButtonType="Image" ImageUrl="/images/crud/delete.png" HeaderImageUrl="/images/crud/delete.png" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="DetailsPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:DetailsView ID="Details" GridLines="None" runat="server"
         AutoGenerateRows="true" DefaultMode="ReadOnly" AllowPaging="false">
        </asp:DetailsView>
    </ContentTemplate>
</asp:UpdatePanel>

this "works": I get a list in the detailsview enumerating the cells I manually built into a class...

what I'd actually want is something similar to

Details.DataSource = Grid.Rows[index]

enumerating the fields and their names, but that doesn't seem to work properly either.

any suggestions or ideas?

this is for a usercontrol, in case it changes anything.