C# 在 gridview 行中应用 foreach 似乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2339462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applying foreach in gridview rows doesn't seem to work
提问by ACP
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....
我有一个 gridview,我正在将 gridview 行转换为数据表......但我无法获得 gridview 内 cell[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);
}
I got the error in the line,
我在行中遇到错误,
dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);
dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);
Input String was not in a correct format
Input String was not in a correct format
Note:
笔记:
Cells[0]
is a hiddenfield which contains EmpId....
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>
采纳答案by Joel Etherton
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);
}
}
回答by SLaks
This error means that cells[0].Text
doesn't contain a number.
此错误意味着cells[0].Text
不包含数字。
Check the value of row.Cells[0].Text
in the debugger.
检查row.Cells[0].Text
调试器中的值。