从字符串到整数的System.InvalidCastException [英] System.InvalidCastException from string to integer

查看:76
本文介绍了从字符串到整数的System.InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨其他编码员,

试图弄清楚为什么我会收到施法错误?我不是要把它转换为整数



这是抛出的错误:

System.InvalidCastException:Conversion从字符串Page1数据到类型''整数''无效。



我的目标是将数组的每个项目作为Listview项目



代码片段:

Hi fellow coders,
trying to figure out why I get a cast error? I''m not trying to cast this to an integer

This is the error thrown:
System.InvalidCastException: Conversion from string "Page1 Data" to type ''Integer'' is not valid.

My objective is to get each item of the array as a Listview Item

code snippet:

Try

           Dim itmListViewItem As ListViewItem
           Dim monk(2) As String
           monk(0) = "Page1 Data"
           monk(1) = "Page2 Data"
           monk(2) = "Page3 Data"

           For x = 0 To 2
               itmListViewItem = New ListViewItem(monk(x))
               ListView1.Items.Add(itmListViewItem)
           Next

       Catch ex As Exception
           Label3.Text = (ex.ToString)
       End Try





来自aspx页面的listview控件:



listview control from aspx page:

<asp:ListView ID="ListView1" runat="server" Visible="False">
    <AlternatingItemTemplate>
          <div id="Div3" runat="server"></div>
           <%# Eval("itmListViewItem")%>
          </div>
      </AlternatingItemTemplate>
      <EditItemTemplate>
          <div id="Div4" runat="server"></div>
           <%# Eval("itmListViewItem")%>
          </div>
      </EditItemTemplate>
      <EmptyDataTemplate>
          <span>No data was returned.</span>
      </EmptyDataTemplate>
      <InsertItemTemplate>
          <div id="Div2" runat="server"></div>
           <%# Eval("itmListViewItem")%>
          </div>
      </InsertItemTemplate>
      <ItemTemplate>
            <div id="Div1" runat="server"></div>
           <%# Eval("itmListViewItem")%>
          </div>
      </ItemTemplate>
      <LayoutTemplate>
          <div ID="itemPlaceholderContainer" runat="server" style="">
              <span runat="server" id="itemPlaceholder" />
          </div>
          <div style="">
              <asp:DataPager ID="DataPager" runat="server" PageSize="2">
                  <Fields>
                      <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="False"

                          ShowNextPageButton="False" ShowPreviousPageButton="True" />
                      <asp:NumericPagerField />
                      <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="False"

                          ShowNextPageButton="True" ShowPreviousPageButton="False" />
                  </Fields>
              </asp:DataPager>
          </div>
      </LayoutTemplate>
      <SelectedItemTemplate>
          <p>
           <%# Eval("itmListViewItem")%>
          </p>
      </SelectedItemTemplate>
  </asp:ListView>





仅供参考:我使用的是ListView Web服务器控件,而不是system.windows.forms中的Listview。



FYI:I am using the ListView Web Server Control, not the Listview within system.windows.forms.

推荐答案

您的字符串包含一个数字,它不是数。你可以使用正则表达式来输出一个数字,如果它总是在同一个地方,则可以使用字符串混合。
Your string contains a number, it''s not a number. You can use regex to pull a number out, or string mashing if it''s always in the same spot.


这篇关于从字符串到整数的System.InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆