Search code examples
c#gridviewforeachrow

Applying foreach in gridview rows doesn't seem to work


I have a gridview and i am converting gridview rows to a datatable... But i cant able to get the value of a hiddenfield in cell[0] inside the gridview....

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

I got the error in the line,

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

Input String was not in a correct format

Note:

Cells[0] is a hiddenfield which contains 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>

My gridview, alt text


Solution

  • You need to add a conditional to make sure you're not parsing the header and the footer:

    EDIT: Working result (leaving the other one because it may also apply to similar situations

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

    EDIT: Since I don't have studio on this to correct myself

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