none
【ASP.NET】GridView中如何隔行显示数据? RRS feed

  • 问题

  • 谁知道,GridView中如何隔行显示数据?
    如图:当GridView数据循环到行业资讯4的时候,空一行,之后再接着显示,请问这样如何做到呢?请高手指教,谢谢!

    我是这样写的,但不起作用,代码如下:


                DataSet dss = OperateDB.GetDataSet("select * from table", CommandType.Text, "table");

                DataTable dt=dss.Tables[0];
                if (dss.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
                    {
                        if (i == 3) //我想在第三行位置,插入一个空行
                        {

                            DataRow dr = dt.NewRow();
                            dt.Rows.Add(dr); 

                        }
                    }
                }
                this.GridView.DataSource = dt;
                this.GridView.DataBind();

     

    dt.Rows.InsertAt(dr, i); 添加到指定行可以了,但问题又来了,每行数据的前面有个“· ” 这样的标志,这是由模板列生成的,怎么样才能清除某一行的模板列呢?

    • 已移动 肖小勇Moderator 2010年4月1日 1:47 asp.net 相关问题 (发件人:.NET Framework 一般性问题讨论区)
    2010年3月31日 13:47

答案

  •  照前面的方法我做了写了一下((Label)this.zixun2.Rows[i].Cells[0].Controls[0]).Text = "";  运行时说索引超出最大范围,不理解。按照我写的应该是对的呀,HTML代码如下:

     <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="dd" runat="server" >d</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

     

    ((Label)this.zixun2.Rows[i].Cells[0].Controls[1]).Text = "";  而这个就是在第几行的第一个单元格中的第一个1个控件,找到后转变为LABLE,然后把它的text属性赋为空。因为Label是模板列所以控件Controls集合应该是第一项,本以为这样会成功,但还是出错,不知道我错在哪里?请大家帮我分析分析,谢谢!


    总算让我搞定了,分享一下代码:

    protected void zixun2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (GridViewRow rv in zixun2.Rows)
                {
                    if (rv.RowIndex == 3)
                    {
                        rv.Cells[1].Controls.Clear();
                        rv.Cells[0].Controls.Clear();
                    }

                }
            }
        }

    同时模板列中应该是一个控件,如下:

    <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" OnRowDataBound="zixun2_RowDataBound" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="d" runat="server" >·</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

    否则不会起到效果

    • 已标记为答案 jinwb1982 2010年4月2日 12:04
    • 已标记为答案 jinwb1982 2010年4月2日 12:04
    2010年4月2日 12:03

全部回复

  • 你好!

    这样做你很难维护,如果只是想达到显示更直观的目的可以考虑每 4 行换一种背景色来代替。


    知识改变命运,奋斗成就人生!
    2010年4月1日 1:46
    版主
  • 那么肖先生,我这个问题如果想解决的话,应该如何做呢?等待回复,感谢!

    2010年4月1日 2:08
  • 如果你是想插入控行,参考 KeFang Chen 版主在下面地址的回复

    http://social.microsoft.com/Forums/zh-CN/295/thread/f14df01d-b724-42bd-9d89-ec649678e05f

    如果是想使用每隔4行显示不同颜色这就需要你在 RowDataBound 事件中自己维护了。

    如果还有其它地方不清楚,请具体说明我们再讨论。


    知识改变命运,奋斗成就人生!
    2010年4月1日 2:14
    版主
  • 你还可以使用Repeater或者ListView控件,比这个简单啊
    【孟子E章】
    2010年4月1日 3:21
    版主
  •  照前面的方法我做了写了一下((Label)this.zixun2.Rows[i].Cells[0].Controls[0]).Text = "";  运行时说索引超出最大范围,不理解。按照我写的应该是对的呀,HTML代码如下:

     <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="dd" runat="server" >d</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

     

    ((Label)this.zixun2.Rows[i].Cells[0].Controls[1]).Text = "";  而这个就是在第几行的第一个单元格中的第一个1个控件,找到后转变为LABLE,然后把它的text属性赋为空。因为Label是模板列所以控件Controls集合应该是第一项,本以为这样会成功,但还是出错,不知道我错在哪里?请大家帮我分析分析,谢谢!

    2010年4月2日 8:18
  •  照前面的方法我做了写了一下((Label)this.zixun2.Rows[i].Cells[0].Controls[0]).Text = "";  运行时说索引超出最大范围,不理解。按照我写的应该是对的呀,HTML代码如下:

     <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="dd" runat="server" >d</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

     

    ((Label)this.zixun2.Rows[i].Cells[0].Controls[1]).Text = "";  而这个就是在第几行的第一个单元格中的第一个1个控件,找到后转变为LABLE,然后把它的text属性赋为空。因为Label是模板列所以控件Controls集合应该是第一项,本以为这样会成功,但还是出错,不知道我错在哪里?请大家帮我分析分析,谢谢!


    总算让我搞定了,分享一下代码:

    protected void zixun2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (GridViewRow rv in zixun2.Rows)
                {
                    if (rv.RowIndex == 3)
                    {
                        rv.Cells[1].Controls.Clear();
                        rv.Cells[0].Controls.Clear();
                    }

                }
            }
        }

    同时模板列中应该是一个控件,如下:

    <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" OnRowDataBound="zixun2_RowDataBound" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="d" runat="server" >·</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

    否则不会起到效果

    2010年4月2日 12:02
  •  照前面的方法我做了写了一下((Label)this.zixun2.Rows[i].Cells[0].Controls[0]).Text = "";  运行时说索引超出最大范围,不理解。按照我写的应该是对的呀,HTML代码如下:

     <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="dd" runat="server" >d</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

     

    ((Label)this.zixun2.Rows[i].Cells[0].Controls[1]).Text = "";  而这个就是在第几行的第一个单元格中的第一个1个控件,找到后转变为LABLE,然后把它的text属性赋为空。因为Label是模板列所以控件Controls集合应该是第一项,本以为这样会成功,但还是出错,不知道我错在哪里?请大家帮我分析分析,谢谢!


    总算让我搞定了,分享一下代码:

    protected void zixun2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (GridViewRow rv in zixun2.Rows)
                {
                    if (rv.RowIndex == 3)
                    {
                        rv.Cells[1].Controls.Clear();
                        rv.Cells[0].Controls.Clear();
                    }

                }
            }
        }

    同时模板列中应该是一个控件,如下:

    <asp:GridView ID="zixun2" runat="server" Width="280px" AutoGenerateColumns="False"
                                                                                    GridLines="None" ShowHeader="False" OnRowDataBound="zixun2_RowDataBound" >
                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="d" runat="server" >·</asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle HorizontalAlign="Left" Width="15px" />
                                                                                        </asp:TemplateField>
                                                                                        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="shownews.aspx?nid={0}"
                                                                                            DataTextField="title" Target="_blank">
                                                                                            <ItemStyle HorizontalAlign="Left" />
                                                                                        </asp:HyperLinkField>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time","{0:yyyy-MM-dd}")%>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="#756F63" HorizontalAlign="Right" />
                                                                                        </asp:TemplateField>
                                                                                    </Columns>
                                                                                    <EmptyDataTemplate>
                                                                                        <div style="width: 100%">
                                                                                            还没有数据</div>
                                                                                    </EmptyDataTemplate>
                                                                                </asp:GridView>

    否则不会起到效果

    • 已标记为答案 jinwb1982 2010年4月2日 12:04
    • 已标记为答案 jinwb1982 2010年4月2日 12:04
    2010年4月2日 12:03