从数据网格中选定的数据行获取 CSV 字符串的优雅方法
我有一个包含系统配置文件名称的数据表。然后,该表被指定为数据网格的数据源,管理员将在其中将多行标记为选定。
我想将选定的配置文件名称存储为逗号分隔的字符串。 我打算这样做:
string AllowedProfiles = string.Empty;
// Retrieve the selected profiles from the data grid
DataRow[] profiles = (this.dgv_SecurityProfiles.DataSource as System.Data.DataTable).Select("profile_OK = 1");
// Ensure we have some data to assign to the CSV string
if( profiles != null && profiles.Length > 0 )
{
// Build the CSV string of the selected names.
foreach( DataRow row in profiles )
{
// Use the second column in the DataTable (first is the check column)
AllowedProfiles += string.Format("{0},", row[1]);
}
// Remove the last comma
AllowedProfiles = AllowedProfiles.Substring(0, AllowedProfiles.Length - 1);
}
虽然上面的代码有效,但我不认为这是最优雅的方法。
关于如何改进它有什么建议吗?
I have a datatable containing system profile names. This table is then assigned as the data source of a data grid where the administrator will mark multiple rows as selected.
I'd like to store the selected profile names as a comma separated string.
I was going to do it like this:
string AllowedProfiles = string.Empty;
// Retrieve the selected profiles from the data grid
DataRow[] profiles = (this.dgv_SecurityProfiles.DataSource as System.Data.DataTable).Select("profile_OK = 1");
// Ensure we have some data to assign to the CSV string
if( profiles != null && profiles.Length > 0 )
{
// Build the CSV string of the selected names.
foreach( DataRow row in profiles )
{
// Use the second column in the DataTable (first is the check column)
AllowedProfiles += string.Format("{0},", row[1]);
}
// Remove the last comma
AllowedProfiles = AllowedProfiles.Substring(0, AllowedProfiles.Length - 1);
}
Whilst the above code works I don't feel it's the most elegant way forward.
Any suggestion on how I could improve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用这个 linq 查询
You can use this linq query