DataTable DataView - 数据库中的位值

时间:2012-09-28 17:49:39

标签: c# datatable bit dataview

我已经在我的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设置为输入Boolean。我需要做什么?

2 个答案:

答案 0 :(得分:2)

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

Admin = datarowviewUsers["Administrator"].ToString() == "True" ? true : false

答案 1 :(得分:0)

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

Admin = datarowviewUsers["Administrator"].ToString() == "1" ? true : false;