我正在开发一个项目,其中我有一个表单,通过它我可以编辑列表视图中可用的问题。{"Input string was not in a correct format."}
。
我的表格代码frmFormWizard
的"编辑"按钮如下:
frmFormWizard.cs Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace SurveyBuilder
{
public partial class frmFormWizard : Form
{
int intPanelNumber = 1;
Boolean blnCancel = false;
//int intFlag = 1;
public frmFormWizard()
{
InitializeComponent();
}
...
private void btnEditTwoOrMoreOptions_Click(object sender, EventArgs e)
{
int QuestionID;
string sql;
QuestionID = Convert.ToInt32(lvTwoOrMoreOptions.SelectedItems[0].Text.ToString());
{
SqlConnection cn = new SqlConnection();
SqlCommand rs = new SqlCommand();
SqlDataReader sdr = null;
clsConnection clsCon = new clsConnection();
clsCon.fnc_ConnectToDB(ref cn);
sql = "";
sql += "SELECT * FROM SurveyQuestionLog WHERE SurveyQuestionLog.QuestionLogID = "+ QuestionID +"";
//sql += "SELECT * FROM SurveyQuestionLog";
rs.Connection = cn;
rs.CommandText = sql;
sdr = rs.ExecuteReader();
while (sdr.Read())
{
txtTwoOrMoreQuestions.Text = (string)sdr["Question"];
txtOption1.Text = (string)sdr["Choice1"];
...
}
sdr.Close();
rs = null;
cn.Close();
}
}
每当我尝试编译代码时,它都会说"{"Input string was not in a correct format."}"
该错误显示在以下行中:
QuestionID = Convert.ToInt32(lvTwoOrMoreOptions.SelectedItems[0].Text.ToString());
请让我知道我做错了什么。
答案
看起来文本中包含一些空格。
lvTwoOrMoreOptions.SelectedItems[0].Text.ToString().Trim()
并转换为int32。
希望这段代码能解决你
来自评论
如果您的 ListView 处于报告模式(即它看起来像网格),那么您将需要 SubItems 属性。lvTwoOrMoreOptions.SelectedItems
获取列表视图中的每个项目 - SubItems 获取列。lvTwoOrMoreOptions.SelectedItems[0].SubItems[0]
是第一列值,