合并gridview单元格不起作用

时间:2017-05-30 03:15:06

标签: c# asp.net gridview merge

我遇到了问题,我尝试合并gridview中的单元格,这些单元格从templatefield生成,但它无效。单元格是gridview中的第一个包含日期时间的单元格,但我在gridview中绑定之前消除了时间。我想在不考虑时间的情况下向用户显示相同日期的不同值。我得到的这个代码也在考虑时间,虽然我已经消除了时间:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnPreRender="GridView1_PreRender" >
        <Columns>
            <asp:TemplateField HeaderText="Request Date">
                <ItemTemplate><%#Eval("request_date")%></ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A01">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A01" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""  %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A02">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A02" ?"Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A03">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A03" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A04">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A04" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A05">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A05" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A06">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A06" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A07">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A07" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""  %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A08">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A08" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A09">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A09" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A10">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A10" ?"Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A11">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A11" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A12">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A12" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A13">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A13" ?"Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A14">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A14" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""  %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A15">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A15" ?"Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A16">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A16" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A17">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A17" ?"Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="A18">
                <ItemTemplate>
                    <%#Eval("prod_line").ToString() == "A18" ? "Job Order: "+Eval("jo_no") +"\n\n\n\n\nQuantity: "+Eval("qty")+"\n\n\n\nNeed by Date: "+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : ""   %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
代码背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    con.Open();
    DataTable dt = new DataTable("CutPanelCard");
    query = "select CONVERT(VARCHAR(10),a.request_date ,101) as request_date, a.prod_line,a.jo_no,a.qty,CONVERT(VARCHAR(10),a.need_by_date ,101) as need_by_date, b.process_id from CutPanelCard a LEFT JOIN CutPanelConfirmation b on b.req_id=a.req_id where a.request_date >= DATEADD(day,-8, GETDATE()) order by a.request_date";

    cmd = new SqlCommand(query, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);

    GridView1.CellPadding = 10;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    //GridView_Row_Merger(GridView1);
    con.Close();
}

我得到的输出: output that i get :

我想要的输出:

output that i want

2 个答案:

答案 0 :(得分:1)

GridView的请求日期列更改为BoundField

<asp:BoundField HeaderText="Request Date" DataField="request_date" />

现在尝试在 GridView RowDataBound

中执行类似的操作
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = e.Row.RowIndex;
            GridViewRow prevRow = null;
            GridViewRow currentRow = e.Row;
            if (index > 0)
            {
                int PrevIndex = index - 1;
                prevRow = (GridViewRow)gvData.Rows[PrevIndex];
            }
            if (prevRow != null)
            {
                if (prevRow.Cells[0].Text == currentRow.Cells[0].Text && (prevRow.Cells[0].Text != "" && currentRow.Cells[0].Text != ""))
                {
                    prevRow.Cells[0].RowSpan += 2;
                    currentRow.Cells.RemoveAt(0);
                }
                else
                {
                    prevRow.Cells[0].RowSpan += 0;
                }
            }
        }
    }

答案 1 :(得分:0)

尝试在后面的代码中使用它。

protected void OnDataBound(object sender, EventArgs e)
{
    for (int i = GridView1.Rows.Count - 1; i > 0; i--)
     {
         GridViewRow row = GridView1.Rows[i];
         GridViewRow previousRow = GridView1.Rows[i - 1];
         for (int j = 0; j < row.Cells.Count; j++)
         {
             if (row.Cells[j].Text == previousRow.Cells[j].Text)
             {
                 if (previousRow.Cells[j].RowSpan == 0)
                 {
                     if (row.Cells[j].RowSpan == 0)
                     {
                         previousRow.Cells[j].RowSpan += 2;
                     }
                     else
                     {
                    previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                     }
                     row.Cells[j].Visible = false;
                 }
             }
         }
     }
 }