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

时间:2010-02-26 04:14:12

标签: c# gridview foreach row

我有一个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 ....的隐藏字段。

 <asp:TemplateField >
   <HeaderStyle Width="1%" />
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
   <asp:HiddenField ID="HiddenId" runat="server" value='<%#Eval("Emp_Id") %>' />
     <asp:Label ID="LblHiddenId" runat="server" Text='<%#Eval("Emp_Id") %>'></asp:Label>
  </ItemTemplate>
  <ItemStyle Width="1%" CssClass="GridCs" HorizontalAlign="Left" />
  </asp:TemplateField>

我的gridview, alt text http://img109.imageshack.us/img109/6804/gridnewcopy.jpg

2 个答案:

答案 0 :(得分:8)

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

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

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);
    }
}  

答案 1 :(得分:2)

此错误表示cells[0].Text不包含数字。

检查调试器中row.Cells[0].Text的值。