如何在网格视图中对特定列进行分组

时间:2015-03-30 06:49:28

标签: c# html asp.net gridview

在网格视图中,我有一个字段项组。我想将gridview行中的所有Item组值组合在一起,标题名称为Itemgroup。 像标题文本一样,Itemgroup值后面跟着它的值。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center">
    <Columns>
        <asp:TemplateField HeaderText="Item Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("TestItemName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Items Group">
            <ItemTemplate>
                <asp:Label ID="lblGroup" runat="server" Text='<%# Eval("TestItemGroup") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Value">
            <ItemTemplate>
                <asp:TextBox ID="txtItemGroup" runat="server" Text='<%# Eval("ItemGroup") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblGroup12" runat="server" Text='<%# Eval("TestItemID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:Label ID="lblItemValue" runat="server" Text='<%# Eval("TestItemValues") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Default Values">
            <ItemTemplate>
                <asp:Label ID="lblDefaultValues" runat="server" Text='<%# Eval("DefaultValues") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

.cs中的绑定网格视图:

        int ID = Convert.ToInt32(txtID.Text);
        sqlstr = "select * from Test_Items where TestID = '" + ID + "'";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
            btnSend1.Visible = true;
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('Invalid ID (or) No Data to Display..!');", true);
        }

为了更好地理解Ex: http://www.pathology-software.com/images/pathology-software/patholgy-screenshot7.jpg

先谢谢你

2 个答案:

答案 0 :(得分:0)

通过在将数据源绑定到GridView之前将“分隔符记录”添加到数据源,可以注入所需的分隔符行。

以下是我遇到的一个例子:

protected override void Render(HtmlTextWriter writer)
{
    string lastSubCategory = String.Empty;
    Table gridTable = (Table)gvProducts.Controls[0];
    foreach (GridViewRow gvr in gvProducts.Rows)
    {
        HiddenField hfSubCategory = gvr.FindControl("hfSubCategory") as
                                    HiddenField;
        string currSubCategory = hfSubCategory.Value;
        if (lastSubCategory.CompareTo(currSubCategory) != 0)
        {
            int rowIndex = gridTable.Rows.GetRowIndex(gvr);
            // Add new group header row
            GridViewRow headerRow = new GridViewRow(rowIndex, rowIndex,
                DataControlRowType.DataRow, DataControlRowState.Normal);
            TableCell headerCell = new TableCell();
            headerCell.ColumnSpan = gvProducts.Columns.Count;
            headerCell.Text = string.Format("{0}:{1}", "SubCategory",
                                            currSubCategory);
            headerCell.CssClass = "GroupHeaderRowStyle";
            // Add header Cell to header Row, and header Row to gridTable
            headerRow.Cells.Add(headerCell);
            gridTable.Controls.AddAt(rowIndex, headerRow);
            // Update lastValue
            lastSubCategory = currSubCategory;
        }
    }
    base.Render(writer);
}

FULL ARTICLE

答案 1 :(得分:0)

<asp:Literal ID="litDataLoader" runat="server">

        </asp:Literal>
        <asp:GridView ID="gvTemp" runat="server" AutoGenerateColumns="false" BorderWidth="0" ShowHeader="false" ShowFooter="false">
            <Columns>
                <asp:TemplateField ControlStyle-Width="200px">
                    <ItemTemplate>
                        <asp:Label ID="lblTestItemName" runat="server" Text='<%#Eval("TestItemName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ControlStyle-Width="220px">
                    <ItemTemplate>
                        <asp:Label ID="lblTestItemValues" runat="server" Text='<%#Eval("Itm") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ControlStyle-Width="200px">
                    <ItemTemplate>
                        <asp:Label ID="lblDefaultValues" runat="server" Text='<%#Eval("DefaultValues") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

.cs中的绑定网格视图:

public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    private void bindData()
    {

        string Query = "select TestItemID,TestItemName,TestItemValues,DefaultValues,TestID,ItemGroup,isnull(TestItemGroup,'-') as TestItemGroup, (ItemGroup + ' ' + TestItemValues) as Itm from Test_Items where TestID = '" + ViewState["id"].ToString() + "'";
        SqlDataAdapter da = new SqlDataAdapter(Query, con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        var rows = dt.AsEnumerable().Select(s => new { id = s.Field<string>("TestItemGroup"), }).Distinct().ToList();
        int count = rows.Count;
        if (dt.Rows.Count > 0)
        {
            StringBuilder build = new StringBuilder();
            foreach (var row in rows)
            {
                string Name = row.id != "-" ? row.id : " ";
                build.Append("<b>" + Name + "</b>");
                DataTable dts = new DataTable();
                dts = dt.Select("TestItemGroup = '" + row.id + "'").CopyToDataTable();
                gvTemp.Visible = true;

                gvTemp.DataSource = dts;
                gvTemp.DataBind();
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                gvTemp.RenderControl(hw);
                string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
                build.Append(gridHTML);
                gvTemp.Visible = false;
            }
            litDataLoader.Text = build.ToString();
        }
    }