如何在gridview中绑定datalist?

时间:2013-12-30 14:12:36

标签: c# asp.net gridview datalist

我想在gridview控件中绑定datalist

在下图中,步骤1,步骤2,...来自数据库,我想将其绑定在datalist中

我尝试使用以下代码

的.aspx

 <asp:GridView ID="gvRoadMap" runat="server" AutoGenerateColumns="false" Width="100%"
                    Style="border: 0px solid #cdcdcd" OnRowDataBound="gvRoadMap_RowDataBound" border="0"
                    CellSpacing="1" CellPadding="3" AllowSorting="true">
                    <Columns>
                          <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:DataList ID="dlRMStepHeader" runat="server" RepeatDirection="Horizontal">
                                    <ItemTemplate>
                                        <asp:Label ID="lblStepName" Text='<%#Eval("STEPNAME") %>' runat="server"></asp:Label>
                                    </ItemTemplate>
                                </asp:DataList>
                            </HeaderTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:DataList ID="dlRMStepItem" OnItemDataBound="dlRMStepItem_ItemDataBound" runat="server">

                            </asp:DataList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

的.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            objUserInformation = this.Session["USERSECURITYINFO"] as UserSecurityInformation;
            //Presentationlayer_Views_AddRoadMap.USERID = Convert.ToInt64(BusinessEngineFacade.GetBusinessEngineFacade().get_GetSessionUserInformation().UserId);
            //this.hdnRoadMapID.Value = "0";
            if (base.Request.QueryString["ID"] != null)
            {
                trainingMapID = Convert.ToString(base.Request.QueryString["ID"]);
            }
            this.GetRoadMapData();
        }
    }

    private void GetRoadMapData()
    {
        RoadMapManager roadmapMgr = new RoadMapManager();
        DataSet dataSet = roadmapMgr.GetAllRaodMapData(Convert.ToInt64(trainingMapID));
        this.ViewState["RoadMapData"] = dataSet;
        if (dataSet.Tables.Count > 0)
        {
            if (trainingMapID != null)
            {
                this.txtRoadmapName.Text = Convert.ToString(dataSet.Tables[3].Rows[0][0]);
                this.txtDescription.Text = Convert.ToString(dataSet.Tables[3].Rows[0][1]);
                this.chkActive.Checked = Convert.ToBoolean(dataSet.Tables[3].Rows[0][2]);
            }
            else
            {
                this.txtRoadmapName.Text = "";
                this.txtDescription.Text = "";
                this.chkActive.Checked = false;
            }
            this.BindRoadMap(dataSet.Tables[0]);
        }
    }
    private void BindRoadMap(DataTable dt)
    {
        this.gvRoadMap.DataSource = dt;
        this.gvRoadMap.DataBind();
    }

    protected void gvRoadMap_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dataSource = new DataTable();
        DataList dataList = new DataList();
        if (e.Row.RowType == DataControlRowType.Header)
        {
            dataList = (DataList)e.Row.FindControl("dlRMStepHeader");
            dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
            dataList.DataSource = dataSource;
            dataList.DataBind();
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dataList = (DataList)e.Row.FindControl("dlRMStepItem");
            dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
            dataList.DataSource = dataSource;
            dataList.DataBind();
        }
    }



 ![enter image description here][1]

如何在asp.net中设计上面的图像?

任何想法?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您收到“对象引用未设置实例”错误(您在评论中提到),因为您尝试抓取的DataList仅存在于EditItemTemplate中:

<EditItemTemplate>            
    <asp:DataList ID="dlRMStepItem" OnItemDataBound="dlRMStepItem_ItemDataBound" runat="server">
    </asp:DataList>
</EditItemTemplate>

在尝试访问该控件之前,您需要更新if条件以确保GridView处于编辑模式:

if (e.Row.RowType == DataControlRowType.DataRow && gvRoadMap.EditIndex >= 0)
{
    dataList = (DataList)e.Row.FindControl("dlRMStepItem");
    dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
    dataList.DataSource = dataSource;
    dataList.DataBind();
}

对于OnItemDataBound事件未触发的问题,尝试连接if块中的事件处理程序(我知道它已经在你的标记中,但只是为了确定):

if (e.Row.RowType == DataControlRowType.DataRow && gvRoadMap.EditIndex >= 0)
{
    // Bind the event
    dataList.ItemDataBound += dlRMStepItem_ItemDataBound;

    dataList = (DataList)e.Row.FindControl("dlRMStepItem");
    dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
    dataList.DataSource = dataSource;
    dataList.DataBind();
}