简体   繁体   English

输入的字符串格式不正确?

[英]Input string was not in a correct format?

I want to display data in gridview based on check box selection from another grid view. 我想根据另一个网格视图中的复选框选择在gridview中显示数据。 The given below code getting an error. 给定的以下代码出现错误。 The error is Input string was not in a correct format . 错误是Input string was not in a correct format Help me to find a proper solution. 帮助我找到适当的解决方案。 Thank you. 谢谢。

Code : 代码:

protected void btnAssign_Click(object sender, EventArgs e)
   {
    Dictionary<int, string> selectedEmployees = new Dictionary<int, string>();
    foreach (GridViewRow row in GridView1.Rows)
       {
        if (row.RowType == DataControlRowType.DataRow)
            {
               CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
               if (chkRow.Checked)
                {
                      selectedEmployees.Add(int.Parse(row.Cells[0].Text), row.Cells[1].Text); // error
                }

            }

        }

       if (selectedEmployees.Any())
        {
            GridView2.DataSource = selectedEmployees;
        }
   }

ASPX: ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="Employee Name" ReadOnly="True" SortExpression="FirstName" />


<asp:TemplateField HeaderText="Select" SortExpression="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:GridView ID="GridView2" runat="server"> </asp:GridView>

DB: D B:

[EmployeeID]        NVARCHAR (50)  NOT NULL,
[FirstName]         NVARCHAR (150) NULL,

The given below image is showing data in grid view based on drop-down selection. 下图给出了基于下拉选择的网格视图中的数据。 After that I want to select some rows and click assign then I have to display the selected rows in new gridview. 之后,我想选择一些行并单击“分配”,然后我必须在新的gridview中显示选定的行。

Image : 图片 :

在此处输入图片说明

You are parsing the STAFF0023 to int 您正在将STAFF0023解析为int

int.Parse(row.Cells[0].Text)

which result in the error 'Input string was not in a correct format' 导致错误'Input string was not in a correct format'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM