我有一个gridview
我想要的是,
当我插入一行包含某些值时,我想在textbox
gridview
中显示其总值
我尝试的是下面,
我创建了一个用于计算文本框的函数。如下所示
private void GrandPaidTotal()
{
float GTotal = 0f;
for (int i = 0; i < GridPayInfo.Rows.Count; i++)
{
String total = (GridPayInfo.Rows[i].FindControl("txtPayamt") as TextBox).Text;
GTotal += Convert.ToSingle(total);
}
txtTotalPaidAmt.Value = GTotal.ToString();
}
我在Page_load
上调用了这个函数。但是在调试时我得到了错误
对象引用未设置为对象的实例。
在第
行for (int i = 0; i < GridPayInfo.Rows.Count; i++)
请告诉我,如何实施。
更新
填充网格的逻辑
protected void GridPayInfo_InsertCommand(object sender, GridRecordEventArgs e)
{
int iRowCountPay = 0;
if (Session["payInfo"] != null)
{
dtpayInfo = (DataTable)Session["payInfo"];
}
else
{
BindDataTable();
}
iRowCountPay = dtpayInfo.Rows.Count;
DataRow newRowPay = dtpayInfo.NewRow();
newRowPay["SR_NO"] = iRowCountPay + 1;
newRowPay["TYPE_ID"] = Convert.ToString(e.Record["TYPE"]);
//newRowPay["TYPE"] = Convert.ToString(e.Record["TYPE"]);
newRowPay["TYPE"] = CF.ExecuteScaler2("Select type_desc from type_mst where Type_Code = 'AGR' and Type_Abbr ='" + Convert.ToString(e.Record["TYPE"]) + "'").ToString();
newRowPay["VENDOR_NAME"] = Convert.ToString(e.Record["VENDOR_NAME"]);
newRowPay["AGRMT_AMT"] = Convert.ToString(e.Record["AGRMT_AMT"]);
newRowPay["PAYABLE_AMT"] = Convert.ToString(e.Record["PAYABLE_AMT"]);
newRowPay["PAID_AMT"] = Convert.ToString(e.Record["PAID_AMT"]);
newRowPay["EXP_TYPE_E_ID"] = Convert.ToString(e.Record["EXP_TYPE"]);
newRowPay["EXP_TYPE"] = CF.ExecuteScaler2("select DESCRIPTION from fnd_flex_values_vl where FLEX_VALUE_MEANING = '" + Convert.ToString(e.Record["EXP_TYPE"]) + "'").ToString();
newRowPay["REMARKS"] = Convert.ToString(e.Record["REMARKS"]);
newRowPay["TRAN_DT"] = Convert.ToString(e.Record["TRAN_DT"]);
newRowPay["VOUCHER_NO"] = Convert.ToString(e.Record["VOUCHER_NO"]);
newRowPay["CHEQUE_NO"] = Convert.ToString(e.Record["CHEQUE_NO"]);
newRowPay["CHEQUE_DT"] = Convert.ToString(e.Record["CHEQUE_DT"]);
newRowPay["CHQ_FAV_NAME"] = Convert.ToString(e.Record["CHQ_FAV_NAME"]);
dtpayInfo.Rows.Add(newRowPay);
GridPayInfo.DataSource = dtpayInfo;
GridPayInfo.DataBind();
AddToViewState("GridPayInfo");
}
public void AddToViewState(string strGrid)
{
if (strGrid == "GrdPartyInfo")
{
Session["partyInfo"] = dtPartyInfo;
}
if (strGrid == "GridPayInfo")
{
Session["payInfo"] = dtpayInfo;
}
if (strGrid == "GridExpInfo")
{
Session["ExpInfo"] = dtExpInfo;
}
}
答案 0 :(得分:0)
使用OnRowDataBound
事件可能更容易。
int totalCount = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
totalCount += Convert.ToInt32(drv["txtPayamt"]);
}
TextBox1.Text = "Total count is: " + totalCount;
}
更新
做了一些进一步的测试。如果GridView包含更复杂的功能(编辑,插入),上面的代码似乎不能正常工作。它每行调用RowDataBound
多次。
这个新代码段的工作方式是循环行并使用FindControl
。此函数需要在添加新行后调用DataBind。
private void countItemsInGrid()
{
int totalCount = 0;
for (int i = 0; i < SettingsB.Rows.Count; i++)
{
TextBox tb = SettingsB.Rows[i].FindControl("txtPayamt") as TextBox;
try
{
totalCount += Convert.ToInt32(tb.Text);
}
catch
{
}
}
TextBox1.Text = "Total count is: " + totalCount;
}