当前位置:  开发笔记 > 编程语言 > 正文

在gridview行中应用foreach似乎不起作用

如何解决《在gridview行中应用foreach似乎不起作用》经验,为你挑选了1个好方法。

我有一个gridview,我正在将gridview行转换为数据表......但是我无法在gridview中的单元格[0]中获取隐藏字段的值....

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
    dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("DaysPresent", typeof(decimal)));
    dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
    dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
    dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));

    foreach (GridViewRow row in gridEmployee.Rows) 
    {
        DataRow dr = dt.NewRow();
        dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);
        dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
        dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
        dr["DaysPresent"] = Convert.ToDecimal(row.Cells[4].Text);
        dr["OpeningAdvance"] = Convert.ToDouble(row.Cells[5].Text);
        dr["AdvanceDeducted"] = Convert.ToDouble(row.Cells[6].Text);
        dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);
        dr["SalaryGiven"] = Convert.ToDouble(row.Cells[8].Text);
        dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
        dt.Rows.Add(dr);
    }

我收到了错误,

dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);

Input String was not in a correct format

注意:

Cells[0] 是一个包含EmpId的隐藏字段....

 
   
    
    
    
   
     
  
  
  

我的gridview, 替代文字http://img109.imageshack.us/img109/6804/gridnewcopy.jpg



1> Joel Etherto..:

您需要添加条件以确保您没有解析页眉和页脚:

编辑:工作结果(留下另一个因为它也可能适用于类似的情况

foreach (GridViewRow row in gridEmployee.Rows) 
{
    DataRow dr = dt.NewRow();
    dr["EmpId"] = Convert.ToInt64(((Label)cells[0].FindControl("LblHiddenId")).Text);
    dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
    dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
    dr["DaysPresent"] = Convert.ToDecimal(row.Cells[4].Text);
    dr["OpeningAdvance"] = Convert.ToDouble(row.Cells[5].Text);
    dr["AdvanceDeducted"] = Convert.ToDouble(row.Cells[6].Text);
    dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);
    dr["SalaryGiven"] = Convert.ToDouble(row.Cells[8].Text);
    dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
    dt.Rows.Add(dr);
}  

编辑:因为我没有工作室来纠正自己

foreach (GridViewRow row in gridEmployee.Rows) 
{
    if(row.RowType == DataControlRowType.DataRow)
    {
        DataRow dr = dt.NewRow();
        dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);
        dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
        dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
        dr["DaysPresent"] = Convert.ToDecimal(row.Cells[4].Text);
        dr["OpeningAdvance"] = Convert.ToDouble(row.Cells[5].Text);
        dr["AdvanceDeducted"] = Convert.ToDouble(row.Cells[6].Text);
        dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);
        dr["SalaryGiven"] = Convert.ToDouble(row.Cells[8].Text);
        dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
        dt.Rows.Add(dr);
    }
}  

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有