恢复值并传递给GridViewRow

时间:2016-08-30 12:08:49

标签: c# gridview foreach datagridviewrow

我有一个GridViewRow [],它有一些数据,这个数据例如是从上一页选择的4行(图片1)

enter image description here

在标记的复选框之后,用户必须发送到确认页面,在此页面中我需要生成另一个gridviewRow,其中只有4个selected.i&m; m使用会话将值传递给另一个页面,但我不能让它发挥作用,我在foreach中做错了。这是代码。

ASCX

<asp:GridView runat="server" class="Tabelas" Width="698px" ID="grdSimulacao"  AutoGenerateColumns="False">
                                    <HeaderStyle CssClass="TabelasHeader branca-10NN"></HeaderStyle>
                                    <RowStyle CssClass="TabelasBody grid"></RowStyle>
                                    <AlternatingRowStyle CssClass="TabelasBodyAlt grid" BackColor="#EEEEEE"></AlternatingRowStyle>
                                    <Columns>
                                        <asp:BoundField HeaderText="NF" DataField="NUMNOTAFISCAL">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="Emissão" DataField="DTEMISSAO" DataFormatString="{0:dd/MM/yyyy}">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="Vencimento" DataField="DTVENCIMENTO" DataFormatString="{0:dd/MM/yyyy}">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="Dias" DataField="DIASANTEC">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="Valor(R$)" DataField="VALTOTAL" DataFormatString="{0:#,0.00}">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="Encargos(R$)" DataField="VLENCARGOS" DataFormatString="{0:#,0.00}">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="Vlr. Final(R$)" DataField="VLFINAL" DataFormatString="{0:#,0.00}">
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                    </Columns>
                                </asp:GridView>

CS - 评论部分是我尝试过但没有成功的事情。

 public void Carrega_valores()
        {

            string NUMNOTAFISCAL = "";
            string PRAZOPGTO = "";
            GridViewRow[] valoresNovos = new GridViewRow[300];
            valoresNovos = (GridViewRow[])Session["vlColunas"];
            foreach (GridViewRow grdCount in valoresNovos)
            {
                DataTable dt = new DataTable();
                if (grdCount != null)
                {
                    //NUMNOTAFISCAL = grdCount.Cells[1].Text;
                    //PRAZOPGTO = grdCount.Cells[4].Text;
                }

                //GridViewRow row = (GridViewRow)grdCount.Rows[0].Clone();
            }
            //DataRow NewRow = dt.NewRow();
            //NewRow[1] = grdCount.Cells[1].Text;
            //dt.Rows.Add(NewRow);
            //grdSimulacao.DataSource = dt;
            //grdSimulacao.DataBind();
      //  }
                //grdSimulacao.Controls[0].Controls.Add(valoresNovos[0]);
                // grdSimulacao.DataSource = valoresNovos;
                // grdSimulacao.DataBind();
         //   }

        }

1 个答案:

答案 0 :(得分:0)

我想出了办法,这就是我所做的。

 public void Carrega_valores()
        {
            GridViewRow[] valoresNovos = new GridViewRow[300];
            valoresNovos = (GridViewRow[])Session["vlColunas"];
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("NUMNOTAFISCAL"));
            dt.Columns.Add(new DataColumn("DTEMISSAO"));
             dt.Columns.Add(new DataColumn("DTVENCIMENTO"));
             dt.Columns.Add(new DataColumn("DIASANTEC"));
            dt.Columns.Add(new DataColumn("VALTOTAL"));
             dt.Columns.Add(new DataColumn("VLENCARGOS"));
            dt.Columns.Add(new DataColumn("VLFINAL"));
            foreach (GridViewRow grdCount in valoresNovos)
            {
                //dt = (DataTable)Session["vlColunas"];
                if (grdCount != null)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = grdCount.Cells[1].Text;
                    dr[1] = grdCount.Cells[2].Text;
                    dr[2] = grdCount.Cells[3].Text;
                    dr[3] = grdCount.Cells[7].Text;
                    dr[4] = grdCount.Cells[5].Text;
                    dr[5] = grdCount.Cells[8].Text;
                    dr[6] = grdCount.Cells[5].Text;
                   //dr 7 é valor de 5 menos 8
                    dt.Rows.Add(dr);
                }
            }
            grdSimulacao.DataSource = dt;
            grdSimulacao.DataBind();
        }