C#从json文件数据中获取的列表框中填充文本框
我从本地文件夹中有一个JSON文件:
shawn Milke.json
{
"FirstName": "Shawn",
"LastName": "Milke",
"Email": "[email protected]",
"TeamName": "Shawn Milke",
"Password": "shawn123",
"IsActive": "Yes",
"UserId": 100
}
这是从JSON文件填充列表框的代码:
private void ShowListViewNew()
{
List<UserAccount> userAccounts = new List<UserAccount>();
var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var outputDir = Path.Combine(dir, "Output");
foreach (var file in Directory.GetFiles(outputDir, "*.json"))
{
var text = File.ReadAllText(file);
var model = JsonConvert.DeserializeObject<UserAccount>(text);
userAccounts.Add(model);
}
listBoxUsers.DataSource = userAccounts;
}
这也是我从列表框中填充文本框的代码:
private void listBoxUsers_SelectedIndexChanged(object sender, EventArgs e)
{
var model = (UserAccount)listBoxUsers.SelectedItem;
txtFirstName.Text = model.FirstName;
txtLastName.Text = model.LastName;
txtEmail.Text = model.Email;
txtTeamName.Text = model.TeamName;
txtPassword.Text = model.Password;
if (model.IsActive== "Yes")
{
chkIsActive.Checked = true;
}
else
{
chkIsActive.Checked = false;
}
txtUserId.Text = model.UserId;
}
现在是我的主要问题我想知道如何将其从INT转换为在此代码行中的字符串,
txtUserId.Text = model.UserId;
因为我通常以这种方式进行操作:
UserId = int.Parse(txtUserId.Text)
从现在开始,我不确定如何将其从int转换为字符串。有人可以帮忙吗?谢谢!
I have a JSON file from my local folder:
Shawn Milke.json
{
"FirstName": "Shawn",
"LastName": "Milke",
"Email": "[email protected]",
"TeamName": "Shawn Milke",
"Password": "shawn123",
"IsActive": "Yes",
"UserId": 100
}
Here's the code in populating the listbox from JSON file:
private void ShowListViewNew()
{
List<UserAccount> userAccounts = new List<UserAccount>();
var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var outputDir = Path.Combine(dir, "Output");
foreach (var file in Directory.GetFiles(outputDir, "*.json"))
{
var text = File.ReadAllText(file);
var model = JsonConvert.DeserializeObject<UserAccount>(text);
userAccounts.Add(model);
}
listBoxUsers.DataSource = userAccounts;
}
And here's also my code in populating the textboxes from the listbox selected:
private void listBoxUsers_SelectedIndexChanged(object sender, EventArgs e)
{
var model = (UserAccount)listBoxUsers.SelectedItem;
txtFirstName.Text = model.FirstName;
txtLastName.Text = model.LastName;
txtEmail.Text = model.Email;
txtTeamName.Text = model.TeamName;
txtPassword.Text = model.Password;
if (model.IsActive== "Yes")
{
chkIsActive.Checked = true;
}
else
{
chkIsActive.Checked = false;
}
txtUserId.Text = model.UserId;
}
Now my main problem is I want to know how I can convert it from int to string in this line of code
txtUserId.Text = model.UserId;
Because I usually do it this way:
UserId = int.Parse(txtUserId.Text)
Since now it's changed, I'm not sure on how I can convert it from int to string. Can someone help? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以做到这一点。
1-)一种简单的静态方法 - 或者您可能更喜欢扩展方法 - 对于将来的使用情况,可能是一种适当的方法。
用法:
2-)或简单地转换内联
或
或
所有用法类型是有效的。作为开发人员,这将是您选择在项目中使用的C#版本的选择。
There are few ways to do this.
1-) A Simple static method - or you may prefer extension method- can be a proper way of this for the future usings.
Usage:
2-) Or Simply Convert inline
or
or
All usage types are valid. As a developer, This will be your choice with depending of which C# version you choosen to use in your project.