Domanda

I want to bind datalist inside gridview control

In the below image Step-1,Step-2,... is coming from database and I want to bind it in datalist

I tried with the below code

.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]

How to design the above image in asp.net?

Any Ideas? Thanks in advance.

È stato utile?

Soluzione

You are getting the "Object reference not set an instance" error (that you mentioned in the comments) because the DataList you're trying to grab only exists in your EditItemTemplate:

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

You need to update your if condition to make sure the GridView is in edit mode before you try to access that control:

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();
}

For your issue with the OnItemDataBound event not firing, try connecting the event handler in your if block (I know it's already in your markup, but just to be sure):

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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top