0

我已经在我的 C# 程序中将数据库表读入 DataTable 中,但是在将包含位值的列分配给布尔值时遇到了一些问题。

到目前为止,这是我的代码:

            // Load and Sort Users DataTable
        DataTable datatableUsers = UserMethods.GetUsers().Tables["Users"];
        datatableUsers.DefaultView.Sort = "Name";

        // Filter Based on Search Box
        datatableUsers.DefaultView.RowFilter = "Name LIKE '%" + user + "%' OR Username LIKE '%" + user + "%'"; ;  
        DataView dataviewUsers = datatableUsers.DefaultView;

        // Loop Through Rows and Add UsersGrid to FlowLayoutPael
        foreach (DataRowView datarowviewUsers in dataviewUsers)
        {
            var UsersGrid = new UsersGrid
            {
                ID = (int)datarowviewUsers["ID"],
                Username = datarowviewUsers["Username"].ToString(),
                User = datarowviewUsers["Name"].ToString(),
                Admin = datarowviewUsers["Administrator"].ToString(),
            };
            flowlayoutpanelUsers.Controls.Add(UsersGrid);
            UsersGrid.WasClicked += UsersGrid_WasClicked;
        }

目前,我正在转换 Admin ToString 并在“True”上进行匹配,但这变得非常混乱,我更愿意将 Admin 设置为布尔类型。我需要做什么?

4

2 回答 2

2

您可以使用内联 if 运算符。例如:

Admin = datarowviewUsers["Administrator"].ToString() == "True" ? true : false
于 2012-09-28T18:36:16.860 回答
0

根据 deepee1 的回答,我认为它返回了一点价值。

Admin = datarowviewUsers["Administrator"].ToString() == "1" ? true : false;
于 2013-10-30T16:40:51.833 回答