澳门太阳娱乐集团官网-太阳集团太阳娱乐登录

android-listview嵌套gridview显示不全
分类:脚本专栏

最近实在是太忙了,好几个月没有更新博客了,近来项目需要用到GRIDVIEW嵌套的,在这里跟大家分享一下,大家如有更好的解决方案,请不吝贴出。在ASP.NET中,GridView嵌套可以显示当前选定的父记录同时显示其子记录。例如,你可以创建按条件查找出相应项目的管理项目列表,同时可展开项目查看目前该项目进展到哪一阶段了。

listview嵌套gridview显示不全
listview的item是gridview,gridview是两列,已经重写了listview的onMeacure方法,但是最后一行的gridview的item显示不全,底部差了一点显示不出来,求大神指点

1.之前做的一个demo中需要listview多条目适配,并且出现了listview嵌套gridview,然后就出现了一个小bug,gridview中的数据显示不全,只能显示一行数据。后来自己查了查资料,发现了问题的原因,原来是嵌套的

太阳集团太阳娱乐登录 1

过程中,gridview的高度变得不可控了,所以这里我们需要,继承gridview并且重写一个方法,给gridview重新设置高度。这样就解决了这个小bug。下面是重写的代码。

而这一使用的基本技术就是为父表创建一个GridView,它的每行又嵌入一个GridView,这些子GridView使用TemplateField来插入到父GridView中。唯一需要注意的是,不能在绑定父GridView的同时绑定子GridView,因为此时父行还没有被创建。而子GridView需要等待父GridView的DataBound时间发生。
在这个示例中父GridView定义了自动产生列(AutoGenerateColumns="True"),我这里实际上使用了两个子GridView.

public classMyGridViewextendsGridView {

 <asp:TemplateColumn HeaderText="OPHist">                            <HeaderStyle Width="40px" CssClass="12-head-pt"></HeaderStyle>                                <ItemTemplate>                                    <asp:Image ID="XXX" runat="server" ImageUrl="../image/plus.gif" AlternateText="ProjectHistory">                                    </asp:Image>                                    <asp:DataGrid ID="grdOPHistory" Style="left: 50px" runat="server" AutoGenerateColumns="True"                                        BorderColor="MintCream" BorderWidth="1px" BackColor="White" CellPadding="1" ForeColor="Black">                                        <SelectedItemStyle ForeColor="GhostWhite" BackColor="#CCCCCC"></SelectedItemStyle>                                        <HeaderStyle Font-Bold="True" Width="40px" ForeColor="White" BackColor="#ff6666"></HeaderStyle>                                                                           </asp:DataGrid>                                </ItemTemplate>                            </asp:TemplateColumn>                            <asp:TemplateColumn HeaderText="Status">                                <HeaderStyle Width="40px" CssClass="12-head-pt"></HeaderStyle>                                <ItemTemplate>                                    <asp:Image ID="SHOW" runat="server" ImageUrl="../image/plus.gif" AlternateText="ProjectDetail">                                    </asp:Image>                                     <asp:DataGrid ID="grdProjectDetail1"  onitemdatabound="grdProjectDetail1_ItemDataBound"  Style="left: 50px" runat="server" AutoGenerateColumns="True">                                        <ItemStyle BackColor="#B3D7F1"></ItemStyle>                                        <HeaderStyle Font-Bold="True" Font-Size="10" Height="20px" ForeColor="black" CssClass="A"                                            BackColor="#4791C5"></HeaderStyle>                                                                           </asp:DataGrid>                                </ItemTemplate>                            </asp:TemplateColumn>

publicMyGridView(Context context) {

太阳集团太阳娱乐登录,这些GRIDVIEW都没有直接指定数据源。而是采用编程的形式来实现。下面贴出父GRIDVIEW的DataBound事件。

super(context);

  private void grdProject_ItemDataBound(object sender, DataGridItemEventArgs e)        {            if (e.Item.DataItem is DataRowView)            {                string bindSql = @"EXEC SSM_SP_GETDTL @PROJECTNUM='" + e.Item.FindControl("PROJECTNUM")).Text.Trim() + "', @REV='" + e.Item.FindControl("REV")).Text.Trim() + "'";                base.doDataBind(e.Item.FindControl("grdProjectDetail1"), bindSql);                (e.Item.FindControl("grdProjectDetail1")).Style.Add("display", "none");                bindSql = @"EXEC SSM_SP_GETOPHIST @PROJECTNUM='" + e.Item.FindControl("PROJECTNUM")).Text.Trim() + "', @REV='" + e.Item.FindControl("REV")).Text.Trim() + "'";                base.doDataBind(e.Item.FindControl("grdOPHistory"), bindSql);                (e.Item.FindControl("grdOPHistory")).Style.Add("display", "none");                if (e.Item.FindControl("SHOW") != null)                {                    e.Item.FindControl("SHOW")).Attributes.Add("onclick", "HierarGrid_toggleRow(this,'grdProjectDetail1')");                }                if (e.Item.FindControl("XXX") != null)                {                    e.Item.FindControl("XXX")).Attributes.Add("onclick", "HierarGrid_toggleRow(this,'grdOPHistory')");                }                if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))                {                    string str2 = e.Item.FindControl("PROJECTNUM")).Text.Trim();                    string str3 = e.Item.FindControl("REV")).Text.Trim();                    ImageButton image = (ImageButton)e.Item.FindControl("btnEdit");                    image.Attributes.Add("onclick", "window.open('SSMProjectDetail.aspx?PROJECTNUM=" + str2 + "&REV=" + str3 + "&Mode=Edit','_blank','top=10,left=10,width=1100,height=900,scrollbars=yes,resizable=yes')");                                   }            }        }

//TODO Auto-generated constructor stub

其实说白了也不难,但说不难也不容易。关键是前台面的HierarGrid_toggleRow怎么写。现将代码帖出,有不明白的地方请留言,如对您有帮助,请给个赞。

}

太阳集团太阳娱乐登录 2太阳集团太阳娱乐登录 3

publicMyGridView(Context context, AttributeSet attrs) {

        function HierarGrid_toggleRow(sender, grd) {            if (sender == null)                return;            var state = 1;            //if the hidden row has not already been generated, clone the panel into a new row            var existingRow = window.document.getElementById(sender.id + "showRow");            if (existingRow == null) {                //getting a reference to the table                var table = GetParentElementByTagName(sender, "TABLE");                var index = GetParentElementByTagName(sender, "TR").sectionRowIndex + 1;                //concatenate name of hidden panel => replace "Icon" from sender.id with "Panel"n                if (grd == "grdProjectDetail1") {                    rowDivName = HierarGrid_ReplaceStr(sender.id, "SHOW", grd);                }                else {                    rowDivName = HierarGrid_ReplaceStr(sender.id, "XXX", grd);                }                var rowDiv = window.document.getElementById(rowDivName);                //adding new row to table                var newRow = table.insertRow;                newRow.id = sender.id + "showRow";                //adding new cell to row                var newTD = document.createElement("TD");                if (table.rows[0].cells[0].colSpan > 1)                    newTD.colSpan = table.rows[0].cells[0].colSpan;                else                    newTD.colSpan = table.rows[0].cells.length;                var myTD = newRow.appendChild;                //clone Panel into new cell                var copy = rowDiv;                copy.style.display = "";                myTD.innerHTML = copy.outerHTML;                rowDiv.parentNode.removeChild;                sender.src = HierarGrid_ReplaceStr(sender.src, "plus", "minus");                state = 1;            }            else {                if (existingRow.style.display == "none") {                    existingRow.style.display = "";                    sender.src = HierarGrid_ReplaceStr(sender.src, "plus", "minus");                    state = 1;                }                else {                    existingRow.style.display = "none";                    sender.src = HierarGrid_ReplaceStr(sender.src, "minus", "plus");                    state = 0;                }            }            // ChangeRowState(sender, state);        }        function HierarGrid_ReplaceStr(orgString, findString, replString) {            pos = orgString.lastIndexOf(findString);            return orgString.substr + replString + orgString.substr(pos + findString.length);        }        function GetParentElementByTagName(element, tagName) {            var element = element;            while (element.tagName != tagName)                element = element.parentNode;            return element;        }

super(context, attrs);

View Code

//TODO Auto-generated constructor stub

}

publicMyGridView(Context context, AttributeSet attrs,intdefStyle) {

super(context, attrs, defStyle);

//TODO Auto-generated constructor stub

}

@Override

protected voidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {

//TODO Auto-generated method stub

intexpandSpec= MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);

super.onMeasure(widthMeasureSpec,expandSpec);

}

}

然后在布局文件中,使用我们重新自定义的gridview就可以了,非常简单!

本文由澳门太阳娱乐集团官网发布于脚本专栏,转载请注明出处:android-listview嵌套gridview显示不全

上一篇:Windows下编译OpenSSL 下一篇:没有了
猜你喜欢
热门排行
精彩图文