GridView绑定固定行列的空数据以及加入页尾

来源:互联网 发布:三国杀ol淘宝买银两 编辑:程序博客网 时间:2024/05/19 07:43

public void BindNullGV(GridView gv,int rcount,int ccount)
    {
        DataTable dt = new DataTable("test");
        for (int j = 0; j < ccount; j++)
        {
            string cname = "C" + j.ToString();
            DataColumn col = new DataColumn(cname, typeof(decimal));
            dt.Columns.Add(col);
        }
        DataRow row;
        for (int i = 0; i < rcount; i++)
        {
            row = dt.NewRow();
            for (int c = 0; c < ccount; c++)
            {
                row[c] = c + 1;
            }
            dt.Rows.Add(row);
        }
        gv.DataSource = dt;
        gv.DataBind();
    }

 

 

protected void gvInfor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[6].Text = "Total:";
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].Text = rsn.ReturnTotal(7, gvInfor, "1").ToString();
            e.Row.Cells[9].Text = rsn.ReturnTotal(9, gvInfor, "1").ToString();
        }
    }

 

 

 public decimal ReturnTotal(int col, GridView gv,string status)
    {
        decimal char_total = 0;
        string strqty = null;
        int textid = 0;
        foreach (GridViewRow gvr in gv.Rows)
        {
            if (status == "1")
            {
                textid = col + 1;
                if (null != ((TextBox)gvr.Cells[col].FindControl("TextBox" + "" + textid.ToString() + "")).Text.Trim())
                {
                    if ("" == ((TextBox)gvr.Cells[col].FindControl("TextBox" + "" + textid.ToString() + "")).Text.Trim())
                    {
                        strqty = "0";
                    }
                    else
                    {
                        strqty = ((TextBox)gvr.Cells[col].FindControl("TextBox" + "" + textid.ToString() + "")).Text.Trim();
                    }
                    char_total += Convert.ToDecimal(strqty);

                }
            }
            else
            {
                if (null != gvr.Cells[col].Text.ToString())
                {
                    if ("" == gvr.Cells[col].Text.Trim())
                    {
                        strqty = "0";
                    }
                    else
                    {
                        strqty = gvr.Cells[col].Text.Trim();
                    }
                    char_total += Convert.ToDecimal(strqty);

                }
            }
        }

        return char_total;

    }

原创粉丝点击