ACP*_*ACP 2 c# foreach gridview 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);
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误,
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>
Run Code Online (Sandbox Code Playgroud)
我的gridview, 替代文字http://img109.imageshack.us/img109/6804/gridnewcopy.jpg
您需要添加条件以确保您没有解析页眉和页脚:
编辑:工作结果(留下另一个因为它也可能适用于类似的情况
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:因为我没有工作室来纠正自己
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);
}
}
Run Code Online (Sandbox Code Playgroud)