【问题标题】:Two rows in asp.net gridview itemtemplate and footertemplateasp.net gridview itemtemplate和footertemplate中的两行
【发布时间】:2017-04-24 17:08:13
【问题描述】:

我有一个页面,我需要在其中创建一个视图,我们可以在其中添加行,并且该视图需要看起来像这样。

这里第二行有一个大文本框。在填充数据并单击添加新链接时,该行应该保存在 DB 中并且可见,并且应该出现一个新的页脚行来填充新数据。 我通过 google 和 SO 找到了 ListView、DataList,但我无法找到实现这一目标的方法。我知道这个问题可能是重复的。但我想通过屏幕截图显示我需要什么。

请多多帮助我并指导正确的方向。谢谢。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以在 GridView 的 RowCreated 事件中执行此操作。但是,额外的行将放置在正常的页眉和页脚行之上。如果需要,您还可以在此处将控件添加到额外的行。但请记住,必须在每个 PostBack 上重新创建动态创建的控件,因此数据绑定不得在 IsPostBack 检查内。

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //cast the sender back to a gridview
        GridView gv = sender as GridView;
    
        //check if the row is the header row
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //create a new row
            GridViewRow extraHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            extraHeader.BackColor = Color.Red;
    
            //loop all the columns and create a new cell for each
            for (int i = 0; i < gv.Columns.Count; i++)
            {
                TableCell cell = new TableCell();
                cell.Text = "ExtraHeader " + i;
    
                //add the cell to the new header row
                extraHeader.Cells.Add(cell);
            }
    
            //add the new row to the gridview
            gv.Controls[0].Controls.AddAt(0, extraHeader);
        }
    
        //check if the row is the footer row
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //create a new row
            GridViewRow extraFooter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
            extraFooter.BackColor = Color.Green;
    
            //add one cell with colspan 2
            TableCell cell1 = new TableCell();
            cell1.Text = "ExtraFooter 1";
            cell1.ColumnSpan = 2;
            extraFooter.Cells.Add(cell1);
    
            //add another one with colspanning the rest 
            TableCell cell2 = new TableCell();
            cell2.Text = "ExtraFooter 2";
            cell2.ColumnSpan = gv.Columns.Count - 2;
            extraFooter.Cells.Add(cell2);
    
            //add +2 to the row count. one for the extra header and 1 for the extra footer
            int insertIndex = gv.Rows.Count + 2;
    
            //add the new row to the gridview
            gv.Controls[0].Controls.AddAt(insertIndex, extraFooter);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-24
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多