zoukankan      html  css  js  c++  java
  • 动态创建gridview列

    gdvSaleVoucher.Columns.Clear();

    DataTable dt = new DataTable(); string strSql = @"select [level] from dbo.Film_FilmRelation group by [level] order by [level] desc "; using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalConnectionString"].ConnectionString)) { SqlDataAdapter cmd = new SqlDataAdapter(strSql, conn); cmd.Fill(dt); } for (int i = 0; i < dt.Rows.Count; i++) { BoundField addbf = new BoundField();// addbf.DataField = "StickCarQutity" + (i + 1); addbf.DataFormatString = "{0:0.00#######}"; addbf.FooterStyle.CssClass = "text_right"; addbf.ItemStyle.CssClass = "text_right"; gdvSaleVoucher.Columns.Add(addbf);
    } BoundField lastbf
    = new BoundField(); lastbf.DataField = "StickCarQutity"; lastbf.DataFormatString = "{0:0.00#######}"; lastbf.FooterStyle.CssClass = "text_right"; lastbf.ItemStyle.CssClass = "text_right"; gdvSaleVoucher.Columns.Add(lastbf);
    //给该列命名
    gdvSaleVoucher.HeaderText = "总数量";

    gdvSaleVoucher.DataSource = dt;
    gdvSaleVoucher.DataBind();
     

    html

    <asp:GridView ID="gdvSaleVoucher" runat="server" AutoGenerateColumns="False" ShowFooter="True"
            Width="400" OnDataBound="gdvSaleVoucher_DataBound">
            <Columns>
                <asp:BoundField DataField="Customer" HeaderText="名称" SortExpression="Customer">
                    <ItemStyle HorizontalAlign="Center" />
                    <FooterStyle HorizontalAlign="Center" />
                </asp:BoundField>
            </Columns>
        </asp:GridView>

    databound给列命名

     protected void gdvSaleVoucher_DataBound(object sender, EventArgs e)
            {
    
                if (gdvSaleVoucher.Rows.Count > 0)
                {
                    DataTable dt = new DataTable();
                    string strSql = @"select [level] from dbo.Film_FilmRelation group by [level] order by [level] desc ";
    
                    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalConnectionString"].ConnectionString))
                    {
                        SqlDataAdapter cmd = new SqlDataAdapter(strSql, conn);
                        cmd.Fill(dt);
                    }
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        gdvSaleVoucher.HeaderRow.Cells[i + 1].Text = dt.Rows[i]["level"].ToString();//给列命名
                    }
                    gdvSaleVoucher.HeaderRow.Cells[dt.Rows.Count + 1].Text = "总数";
                }
            }
  • 相关阅读:
    锚接口(上)——hashchange api 和 $.uriAnchor
    仿B站项目(4)webpack打包第三方库jQuery
    仿B站项目(3)页面配置
    微信小程序:scroll滑到指定位置
    开发微信小程序——古龙小说阅读器
    仿B站项目——(2)环境配置,文件目录
    仿B站项目——(1)计划,前端工程
    腾讯Alloy团队代码规范
    webpack热加载:修改文件自动刷新浏览器并更新
    日时相克,困龙被伤。日落死地
  • 原文地址:https://www.cnblogs.com/fuge/p/2716707.html
Copyright © 2011-2022 走看看