如何在asp.net的网格视图中插入空白行 [英] How to insert blank row in the grid view in asp.net

查看:71
本文介绍了如何在asp.net的网格视图中插入空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void BindGrid()
    {
        //int iPageSize = 0;
        //int iRow = 0;
        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Demo_eLogsVA4;Integrated Security=True");
        cn.Open();
        string q = "Select Convert(nVarchar,A.Rec_No) CRec,A.Acc_Name Descr,A.Quantity Qty,Convert(nVarchar,A.Sell_Rate) SRate,Sell_CName Sname , Sell_CCode SCCode, Sell_CValue SCValue ,Convert(numeric,A.Quantity*A.Sell_Rate*Sell_CValue) as SAmount, A.Service_Tax STax,Convert(nVarchar,(A.Quantity*A.Sell_Rate*Sell_CValue)* A.Service_Tax/100)Tax_Amount,Convert(numeric,((A.Quantity*A.Sell_Rate*Sell_CValue)+(A.Quantity*A.Sell_Rate*Sell_CValue)* A.Service_Tax/100)) as Net From eLogs_Invoice_Charges_Details A Where A.Invoice_Code=''INV1''";
        //string qq = "Select COUNT(*) From eLogs_Invoice_Charges_Details A Where A.Invoice_Code=''INV1''";
        DataSet dset = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(q, cn);
        //DataTable dtable = new DataTable();
        da.Fill(dtable);
        GridView1.DataSource = dtable;       
        object obj = dtable.Compute("Sum(Net)", "");
        object amt=dtable.Compute("Sum(SAmount)","");
        decimal value=0;
        if (obj != null)
        {
            value = (Decimal)obj;
        }
        else
        {
            // obj is null.
        }
        DataRow dr;
        for (int i = dt.Rows.Count; i < 10; i++)
        {
            dr = dt.NewRow();
            dr[0] = dr[1] = dr[2] = dr[3] = dr[4] = "";
            dt.Rows.Add(dr);
        }
        dt.AcceptChanges();
        //object obj = dtable.Compute("SUM(Net)", string.Empty);       
        //dtable.Rows.Add(new object[] { "", "" });
        //dtable.Rows.Add(new object[] { "", "" });
        GridView1.DataBind();
        //iPageSize = Convert.ToInt32(GridView1.PageCount.ToString());
        //iRow = Convert.ToInt32(GridView1.Rows.Count.ToString());
        GridView1.FooterRow.Cells[1].Text = "Total:";
        GridView1.FooterRow.Cells[7].Text = amt.ToString();
        GridView1.FooterRow.Cells[10].Text = value.ToString();
        //GridView1.FooterRow.Cells[2].Text = iRow.ToString();
        //GridView1.FooterRow.Cells[3].Text = iPageSize.ToString();
        GridView1.FooterStyle.Font.Bold = true;       
        cn.Close();
    }



我在上面的代码中有错误.

对象引用未设置为对象的实例.

说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例.

源错误:



i have error in the above code.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

<br />
Line 50:         }<br />
Line 51:         DataRow dr;<br />
Line 52:         for (int i = dt.Rows.Count; i < 10; i++)<br />
Line 53:         {<br />
Line 54:             dr = dt.NewRow();<br />


此for循环的目的是,如果pagesize不等于网格视图中的页面行数,则插入空白行.


The purpose of this for loop is to insert blank row if pagesize not equal to the pagerows in the grid view

推荐答案



您可以检查这些-

http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx [ http://forums.asp.net/t/1217361.aspx/1 [ ^ ]
Hi,

You can check these-

http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx[^]

http://forums.asp.net/t/1217361.aspx/1[^]


using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Description");

dt.Rows.Add(new object[] { "", "" });

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

}
}


这篇关于如何在asp.net的网格视图中插入空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆